mysqli: Could not fetch mysqli_result

0

I want to save the result of a query in $_SESSION , but when I try to retrieve the query from $_SESSION , the error occurs

  

"Could not fetch mysqli_result".

Note: I use session_start() . Here is the code:

Query mysql:

$user_types = $mysqli->query("SELECT tipo from tipo_usuario WHERE usuario = '$user_id' ORDER BY tipo");

Placing the result in $ _SESSION:

$_SESSION['user_types'] = $user_types;

When I try to retrieve the query by $_SESSION , here is the error:

while($row = $_SESSION['user_types']->fetch_array())
    
asked by anonymous 30.12.2016 / 19:11

1 answer

2

You're trying to get a pointer to an open resource, not a result. It does not work. Or you get the data and save it in an array, or better yet, open the connection and query where to use it.


If you REALLY need the separate page query, do so:

Page A

$_SESSION['sql'] = "SELECT tipo from tipo_usuario WHERE usuario = '$user_id' ORDER BY tipo";

Page B

// abra a conexao nessa pagina aqui 
$user_types = $mysqli->query($_SESSION['sql']);

while($user_types->fetch_array()) {
    ...
    
30.12.2016 / 19:18