Set SQL results to SESSION equivalent

With a table setup as follows:

CID   | FNAM | LNAME | CITY ------> 30 more fields
23432 | John |  Doe  | Denver
54342 | Jane |  Doe  | Boston

and when getting a result set this way (The query narrowed it down to 1 result):

$data = mysqli_query($con,$query);
$row = mysqli_fetch_array($data);

and using below to set each returned column in $row to a equivalent SESSION variable

foreach ($row AS $key=>$value) {$_SESSION["$key"]=$value;}

Why is it that I am getting a matching $_SESSION set with both the $key name, as well as a $_SESSION set with a numeric position in the array for each $row that was set to a session variable?:

echo $_SESSION['cid']; results in 23432
echo $_SESSION['0']; results in 23432

$_SESSION['cid'] is intended, but $_SESSION['0'] is not


Because you are using mysqli_fetch_array not mysqli_fetch_assoc

But anyway, you will get only the last row from your database, because you are always overwrite the given key. Loop through the fieldnames, and create an array, and add that array to a result array.

链接地址: http://www.djcxy.com/p/59732.html

上一篇: 从会话数组中提取两个单变量

下一篇: 将SQL结果设置为与SESSION等效