views:

39

answers:

1

this is my sp:

CREATE DEFINER=`root`@`localhost` PROCEDURE `getproductname`(in productid int , out productname varchar(200))
BEGIN
        select product_name from product where product_id=productid ;
END

and this is my php code:

$mysqli = new mysqli(DB_HOST,DB_USER,DB_PWD,DB_NAME);

$mysqli->query("CALL getproductname(2049,@productname)") ;
$result = $mysqli->query("SELECT @productname");
while($row = $result->fetch_object()){
    echo ($row[0]);
}

and this is error:

Fatal error: Call to a member function fetch_object() on a non-object in D:\WEB\book24ss\_res\sptest.php on line 19

why?

A: 

You have the following problems with your code:

  1. You never assign an value to your out parameter productname inside your stored procedure.
  2. Even if you assigned a value to productname, retrieving it by using SELECT @productname would only result in a single value - there is no need to iterate over the result.
  3. The query CALL getproductname(2049,@productname) will return the result set you'd get from the simple query select product_name from product where product_id=@productid - you'd have to iterate over the result set you get back from calling your stored procedure.

I think you have some error in reasoning regarding the usage and functioning of stored procedures.

Stefan Gehrig
thank you very much.your answer was very usefull
mohammad mirzadeh