PHP_PDO calls the stored procedure and returns parameter issues
$ Stmt = $ db-> prepare ("CALL pro_test (?, @?) "); $ Stmt-> bindParam (1, $ name); $ stmt-> bindParam (2, $ return_value, PDO: PARAM_STR, 4000 ); // Insert a row $ name = 'Chengdu city '; $ stmt-> execute (); print_r ($ return_value );
How can I call the stored procedure? why is the returned parameter null? yes. check the database. data has been successfully added,
Run the following command directly in the database:
SET @ n =-1;
CALL pro_test ("Ah, I plan to", @ n );
Select @ n;
All returned parameters are correct.
Reply to discussion (solution)
When a stored procedure is called, the database may return multiple result sets.
In sequence: the basic select result, the result of Stored Procedure 1, and the result of stored procedure 2 ....
Your SQL command is CALL pro_test (?, @?) No select, so it can only be located in the second result set
If select CALL pro_test (?, @?) It is in the first result set.
Of course, the environment is changing, and you cannot accurately know where the result assembly is.
So we need to read it cyclically.
do { $rows = $stmt->fetchAll(PDO::FETCH_NUM); if ($rows) { print_r($rows); } } while ($stmt->nextRowset());
When a stored procedure is called, the database may return multiple result sets.
In sequence: the basic select result, the result of Stored Procedure 1, and the result of stored procedure 2 ....
Your SQL command is CALL pro_test (?, @?) No select, so it can only be located in the second result set
If select CALL pro_test (?, @?) It is in the first result set.
Of course, the environment is changing, and you cannot accurately know where the result assembly is.
So we need to read it cyclically.
do { $rows = $stmt->fetchAll(PDO::FETCH_NUM); if ($rows) { print_r($rows); } } while ($stmt->nextRowset());
As you said, add the select statement before the SQL statement and check that the database does not have any execution results after execution. data can be successfully added before addition, and no error is reported.
Remove select and add data, but print_r ($ stmt-> fetchAll (PDO: FETCH_NUM); The Returned result is Array ()
I don't know much about it. I hope I can explain it.
You need
SET @ n =-1; CALL pro_test ("Ah, I plan to", @ n); select @ n;
Execute 3 commands
So how can we simplify it into one in php?
You need
SET @ n =-1; CALL pro_test ("Ah, I plan to", @ n); select @ n;
Execute 3 commands
So how can we simplify it into one in php?
So how should I write in PHP? I checked a lot of information, and all the demos in PHP.net were wrong.
You need
SET @ n =-1; CALL pro_test ("Ah, I plan to", @ n); select @ n;
Execute 3 commands
So how can we simplify it into one in php?
Figured it out.
$stmt = $db->prepare("CALL pro_test(?,@sp_result);");$stmt->bindParam(1, $name);;$stmt->execute ();$outputArray = $db->query("select @sp_result")->fetch(PDO::FETCH_ASSOC);
Thank you!