如何在 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();
}
相关文章