如何在PHP中运行bind_param()语句? [英] How to run the bind_param() statement in PHP?

查看:44
本文介绍了如何在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行,但是请检查

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()语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆