简体   繁体   中英

Cant get stored procedure resultset via PDO PHP

I cant seem to get any output from my stored procedure (MYSQL) using PDO (PHP).

Stored procedure:

CREATE PROCEDURE getUserId(serviceId VARCHAR(64), serviceInput VARCHAR(32))
BEGIN
    SELECT id FROM users WHERE servid = serviceId AND service = serviceInput;
END

PHP code:

$mysqlConn = mysqlPDOLogin();
$stmt = $mysqlConn->prepare("CALL getUserId(?,?);");
$stmt->bindParam(1, $USERID); 
$stmt->bindParam(2, $USERPROVIDER); 
$stmt->execute();
$returned_a = $stmt->fetch();
echo $returned_a['id'];

It doesnt get anything back. Ive copied the basic select code out of the stored procedure and used it directly in the PHP code and it works, but cant get it to work via stored procedure.

Any help would be appreciated...

You're not doing any apparent error checking. Prepare(), bindParam() and execute() will all alert you to errors by either returning false or throwing an exception, depending on the error reporting mode PDO is in.

I'm going to assume you're in "return false" mode.

You need to check that $stmt is a PDOStatement object, that the bindParam statements didn't return false and that execute didn't return false. If any of them did then you need to use errorCode() and errorInfo() to find out what went wrong.

Also, it looks somewhat excessive to me to be using a stored procedure for what is basically a simple query. Why not just do the following?

$stmt = mysqlConn->prepare ('SELECT id FROM users WHERE servid = ? AND service = ?');

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