开发者

I having a problem with the mysqli free() member function

I have code where I connected to the database like so:

$db = new mysqli("localhost", "user", "pass", "company");

Now when I query the database like so:

//query calls to a stored procedure 'user_info' 
$result = $db->query("CALL user_info('$instruc', 'c_register', '$eml', '$pass', '')");
if($result->fetch_array())
{
  开发者_高级运维 //use the results
}
$result->close();

$result = $db->query("CALL user_info('$instruc', 's_login', '$eml', '$pass', '')");
if($result->fetch_array())
{
   //use the results
}
$result->close();

I get this error after the first $result->close();

Fatal error: Call to a member function fetch_array() on a non-object in...

For me to run this other query I have to close the db connection and connect again, then it will work. I want to know if there is a way I could run the other query without having to disconnect and reconnect to the database.


free() removes any data stored in connection with the result set, but it doesn't unset the $result object itself, so $result being still set is perfectly okay.

You should still be able to start a new query using the $db object, though. Freeing a result set should not affect the database object itself.

Can you show the full code of the query that fails after you run free() on your result object and what error it returns?


A simple example given below:

$query='select ml_id, list_name from ml_lists order by list_name asc';
$squery=$db->query($query);

    while($rows=$squery->fetch_assoc()){
        echo $rows['list_name'];
    }
}
$squery->close();


Add some error handling. if mysqli->query fails it returns false, so $result isn't an object, hence the Fatal error: Call to a member function xyz() on a non-object.
In that case mysqli->error should contain the error message.

//query calls to a stored procedure 'user_info'
$result = $db->query("CALL user_info('$instruc', 'c_register', '$eml', '$pass', '')");
if ( !$result ) {
  die('an error occured: '.$db->error);
}
if($result->fetch_array())
{
  //use the results
}
$result->close();

$result = $db->query("CALL user_info('$instruc', 's_login', '$eml', '$pass', '')");
if ( !$result ) {
  die('an error occured: '.$db->error);
}
if($result->fetch_array())
{
  //use the results
}
$result->close();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜