tags:

views:

94

answers:

2

Hi

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 conection 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.

thanks in advance.

+2  A: 

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?

Pekka
@pekka I have edited the question, can you please check it out.
mneva
@neo close() closes the database connection. use free() instead, it should work.
Pekka
A: 

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();
VolkerK
@VolkerK I tried that code now I get: "an error occured: Commands out of sync; you can't run this command now".
mneva
Does `while( $db->next_result() );` before each `$result->close();` fix the problem?
VolkerK