Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP mysqli为带out参数的存储过程准备的语句_Php_Mysql_Stored Procedures_Mysqli_Prepared Statement - Fatal编程技术网

PHP mysqli为带out参数的存储过程准备的语句

PHP mysqli为带out参数的存储过程准备的语句,php,mysql,stored-procedures,mysqli,prepared-statement,Php,Mysql,Stored Procedures,Mysqli,Prepared Statement,我有一个存储过程IsUserPresent,如下所示: DELIMITER $$ CREATE PROCEDURE IsUserPresent( in userid varchar (150), out isPresent bit ) BEGIN SET isPresent=0; SELECT COUNT(*) INTO isPresent FROM users_table WHERE users_table.userid=userid;

我有一个存储过程IsUserPresent,如下所示:

DELIMITER $$
CREATE PROCEDURE IsUserPresent(
    in userid varchar (150),
    out isPresent bit
)
BEGIN
    SET isPresent=0;
    SELECT COUNT(*)
    INTO isPresent
    FROM users_table
    WHERE users_table.userid=userid;
END$$
我想使用mysqli prepared语句从PHP调用它。我是按照代码片段做的,但它给了我警告

$connect=&ConnectDB();
$stmt=$connect->prepare("CALL IsUserPresent(?,?)");
$stmt->bind_param('si',$uid,$userCount);
$stmt->execute();
$toRet = $userCount!=0;
Disconnect($connect);
return $toRet;
警告如下:

Premature end of data (mysqlnd_wireprotocol.c:1112)
Warning: mysqli_stmt::execute(): RSET_HEADER packet 1 bytes shorter than expected
Warning: mysqli_stmt::execute(): Error reading result set's header

应该是注释,但由于代码格式,作为答案发布

不能评论PHP代码,我不是程序员,但您的过程应该更像这样:

DELIMITER $$
CREATE PROCEDURE IsUserPresent(
    in p_userId varchar (150),
    out p_isPresent bit
)
BEGIN

    SELECT EXISTS (SELECT 1 FROM users_table WHERE user_table.userid = p_userId) 
    INTO p_isPresent;

END$$
使用
exists()
,因为一旦找到条目它就会停止
count()
继续查找记录,尽管这不是必需的

并且您命名了一个与列名相同的参数。这对MySQL来说是令人困惑的,应该不惜一切代价避免。良好的做法是,在参数前面加上
p
前缀,在变量前面加上
v
前缀和/或一些变量或参数类型的指示

为了更好的可读性,我还将参数名称更改为camel大小写


哦,最后,始终在问题中包含错误消息。

应该是注释,但由于代码格式的原因,将其作为答案发布

不能评论PHP代码,我不是程序员,但您的过程应该更像这样:

DELIMITER $$
CREATE PROCEDURE IsUserPresent(
    in p_userId varchar (150),
    out p_isPresent bit
)
BEGIN

    SELECT EXISTS (SELECT 1 FROM users_table WHERE user_table.userid = p_userId) 
    INTO p_isPresent;

END$$
使用
exists()
,因为一旦找到条目它就会停止
count()
继续查找记录,尽管这不是必需的

并且您命名了一个与列名相同的参数。这对MySQL来说是令人困惑的,应该不惜一切代价避免。良好的做法是,在参数前面加上
p
前缀,在变量前面加上
v
前缀和/或一些变量或参数类型的指示

为了更好的可读性,我还将参数名称更改为camel大小写


哦,最后,总是在问题中包含错误消息。

存储过程处理准备好的语句的方式有点复杂。声明必须使用会话变量(MySQL会话,而不是PHP)

输入输出参数

使用会话变量访问INOUT/OUT参数的值

这样你就可以用

$connect=&ConnectDB();
// bind the first parameter to the session variable @uid
$stmt = $connect->prepare('SET @uid := ?');
$stmt->bind_param('s', $uid);
$stmt->execute();

// bind the second parameter to the session variable @userCount
$stmt = $connect->prepare('SET @userCount := ?');
$stmt->bind_param('i', $userCount);
$stmt->execute();

// execute the stored Procedure
$result = $connect->query('call IsUserPresent(@uid, @userCount)');

// getting the value of the OUT parameter
$r = $connect->query('SELECT @userCount as userCount');
$row = $r->fetch_assoc();               

$toRet = ($row['userCount'] != 0);
备注:


我建议将此过程重写为带有一个IN参数的函数,该参数返回INT。

存储过程处理准备语句的方式要复杂一些。声明必须使用会话变量(MySQL会话,而不是PHP)

输入输出参数

使用会话变量访问INOUT/OUT参数的值

这样你就可以用

$connect=&ConnectDB();
// bind the first parameter to the session variable @uid
$stmt = $connect->prepare('SET @uid := ?');
$stmt->bind_param('s', $uid);
$stmt->execute();

// bind the second parameter to the session variable @userCount
$stmt = $connect->prepare('SET @userCount := ?');
$stmt->bind_param('i', $userCount);
$stmt->execute();

// execute the stored Procedure
$result = $connect->query('call IsUserPresent(@uid, @userCount)');

// getting the value of the OUT parameter
$r = $connect->query('SELECT @userCount as userCount');
$row = $r->fetch_assoc();               

$toRet = ($row['userCount'] != 0);
备注:


我建议将此过程重写为一个函数,其中一个IN参数返回INT。

上面的语句在pÈu中显示了什么。我认为我们不能在外部使用嵌套查询返回的结果。感谢您的提示。
exists()
返回0或1(true或false)。子查询并不是一个问题,因为您要将结果放入一个变量中。我认为我们不能在外部使用嵌套查询返回的结果。感谢您的提示。
exists()
返回0或1(true或false)。子查询不是问题,因为您将结果放入变量中。