PHP_PDO calls the stored procedure and returns parameter issues

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.