php - Looping through MySQL results and saving values to a $_SESSION -


i trying store each column name in database own $_session. example, column names column_one, column_two, column_three, column_four, , column_five. want these stored in $_session $_session['column_one'], $_session['column_two'], etc. trying in loop have not been successful. how setup loop achieve this?

$query = "select * table user_id = $id"; $result = mysqli_query($dbc, $query); $num = mysqli_num_rows($result);  if ($num == 1) {      //user found     while($row = mysqli_fetch_array($result, mysqli_both)) {         $_session['user_id'] = $row['user_id'];     }  } 

something should work:

while($row = mysqli_fetch_array($result, mysqli_assoc)) {     foreach($row $column => $value) {         $_session[$column] = $value;     } } 

extra advice safeguarding against sql injection, following 2 lines:

$id = mysqli_real_escape_string($dbc, $id); $query = "select * table user_id = '$id'"; 

update:

thanks emiliogort pointed out missing connection parameter in mysqli_real_escape_string. see mysqli_real_escape_string docs.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -