views:

42

answers:

2

I have a stored procedure:

delimiter //

create procedure userlogin(in eml varchar(50))

begin

select * from users

where email = eml;

end//

delimiter ;

And the php:

$db = new mysqli("localhost","root","","houseDB");

$eml = "[email protected]";

$sql = $db->query("CALL userlogin('$eml')");

$result = $sql->fetch_array();

The error that I get from the browser when I run the php script: Fatal error: Call to a member function fetch_array() on a non-object...

I am using phpmyadmin version 3.2.4 and mysql client version 5.1.41.

Please help. Thank you.

A: 

mysqli::query returns false if the query fails (instead of returning a result object or true). You need to test whether the result actually is an object:

$sql = $db->query("CALL userlogin('$eml')");

if (is_object($sql)) 
$result = $sql->fetch_array();
else
printf("Error: %s\n", $sql->error);

You will probably get an error message explaining why calling the stored procedure didn*t work out.

Pekka
$sql->error will also fail because `false` has no 'error' attribute.In case of false you can `echo mysql_error();` to debug the error
maid450
@maid450 this is `mysqli`, not `mysql`.
Pekka
right, sorry, didn't realized to that :P
maid450
+2  A: 

You have to use mysqli_multi_query, not query. Check http://us.php.net/manual/en/mysqli.multi-query.php , they have a good example

a1ex07
Why does calling a stored procedure need `mysqli_multi_query`? He isn't creating it, he's just calling it, isn't he?
Pekka
But it returns values. If it didn't, it would work as it is (actually, it's working, but in order to access result set from procedure we need to use multi_query).
a1ex07