简体   繁体   中英

I want get a value from sp

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM