简体   繁体   English

无法通过PDO PHP获取存储过程结果集

[英]Cant get stored procedure resultset via PDO PHP

I cant seem to get any output from my stored procedure (MYSQL) using PDO (PHP). 我似乎无法使用PDO(PHP)从存储过程(MYSQL)获得任何输出。

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: PHP代码:

$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. 我已经从存储过程中复制了基本选择代码,并直接在PHP代码中使用了它,并且可以正常工作,但是无法通过存储过程使其工作。

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. 根据PDO所处的错误报告模式,Prepare(),bindParam()和execute()都会通过返回false或引发异常来提醒您注意错误。

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. 您需要检查$ stmt是PDOStatement对象,bindParam语句没有返回false,而execute没有返回false。 If any of them did then you need to use errorCode() and errorInfo() to find out what went wrong. 如果它们中的任何一个都做了,那么您需要使用errorCode()和errorInfo()来找出问题所在。

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 = ?');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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