mysqli: Couldn’t fetch mysqli_result

Asked

Viewed 309 times

0

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

"Couldn’t fetch mysqli_result".

Note: I use the session_start(). Follows the code:

Query mysql:

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

Putting the result in $_SESSION:

$_SESSION['user_types'] = $user_types;

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

while($row = $_SESSION['user_types']->fetch_array())
  • I believe that it is better to keep the specified value than Resource (return of query()). Basically what you should guard is the return of fetch_array()

1 answer

2

You’re trying to put a pointer to an open resource, not a result. It doesn’t work. Either you take the data and store 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()) {
    ...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.