如何在 PHP 中运行 bind_param() 语句?

时间:2023-03-07
本文介绍了如何在 PHP 中运行 bind_param() 语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试使以下代码工作,但无法到达 execute() 行.

I'm trying to make the following code work but I can't reach the execute() line.

$mysqli = $this->ConnectLowPrivileges();
echo 'Connected<br>';
$stmt = $mysqli->prepare("SELECT `name`, `lastname` FROM `tblStudents` WHERE `idStudent`=?");
echo 'Prepared and binding parameters<br>';
$stmt->bind_param('i', 2 );
echo 'Ready to execute<br>'
if ($stmt->execute()){
    echo 'Executing..';
    }
} else {
    echo 'Error executing!';
}
mysqli_close($mysqli);

我得到的输出是:

Connected
Prepared and binding parameters

所以问题应该在第 5 行,但检查 bind_param() 手册我在那里找不到任何语法错误.

So the problem should be at line 5, but checking the manual of bind_param() I can't find any syntax error there.

推荐答案

绑定参数时需要传递一个变量作为引用:

When binding parameters you need to pass a variable that is used as a reference:

$var = 1;

$stmt->bind_param('i', $var);

参见手册:http://php.net/manual/en/mysqli-stmt.bind-param.php

注意 $var 实际上并不需要定义来绑定它.以下是完全有效的:

Note that $var doesn't actually have to be defined to bind it. The following is perfectly valid:

$stmt->bind_param('i', $var);

foreach ($array as $element)
{

    $var = $element['foo'];

    $stmt->execute();

}

这篇关于如何在 PHP 中运行 bind_param() 语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:你如何在 mysqli 准备好的语句中使用 IN 子句 下一篇:mysqli_multi_query 是异步的吗?

相关文章