how to keep two rows of records in $ _SESSION?


I'm looking for a way to keep in $ _SESSION two or more rows of records that are thrown by a mysql query, and then use those results in other queries. I usually pulled a single record row with:


but that would only keep one row in session now I need you to keep several rows in session of this query:

$consultar=mysql_query("SELECT * from asignacion where cedula='$_SESSION[cedula]'");
  echo "<script>alert('¡NO HAY EQUIPOS ASIGNADOS A ESTE EMPLEADO!')</script>";
  echo "<meta http-equiv='refresh' content='0; url=../vista/devolucion.php'>";


echo "<script>alert('¡ENCONTRADO!')</script>";

and be able to use it to generate a dynamic table with those records found


    $sqlQuery = mysql_query("SELECT * from equipo where id_case='$_SESSION[id_case]'");
    $table = $sqlQuery or die(mysql_error());
    if ($table) {

but everything is dynamic, you do not know how many records the user will make, I hope you can help me and thanks in advance

asked by Bryant Medina 05.11.2016 в 14:48

2 answers


It is understood that you can only record the data of a single user in session so your first query works but you can improve it, you can research mysql_fetch_row

In the second part you are overwriting the results, so you should save it as an arrangement, change to:

while($row = mysql_fetch_array($consultar, MYSQL_ASSOC)) {
    $_SESSION['asignaciones'][] = $row;

To recover it in the dynamic tables you use:

    foreach($_SESSION['asignaciones'] as $asignacion) {
        $query = mysql_query("SELECT * FROM equipo WHERE id_case='".$asignacion['id_case']."'");
       .. genera tabla equipos por id_case ..


answered by 05.11.2016 в 16:39

You can store a multidimensional array on an element of the session:

$_SESSION["asignaciones"] = array();

while($row=mysql_fetch_array($consultar)) {
    $asignacion = array();
    $asignacion["id_case"] = $row["id_case"];
    $asignacion["periferico1"] = $row["periferico1"];
    $asignacion["periferico2"] = $row["periferico2"];
    $asignacion["periferico3"] = $row["periferico3"];
    $asignacion["id_laptop"] = $row["id_laptop"];
    $_SESSION["asignaciones"][] = $asignacion;
answered by 05.11.2016 в 16:29