php bind_param用于多个值

时间:2014-04-30 10:45:56

标签: php mysqli

出于某种原因,当我尝试使用多个绑定参数时,我无法使它们起作用。

$stmt = $mysqli->prepare("SELECT * FROM `course_timetable` WHERE 'student_id'=(SELECT 'student_id' FROM `student_accounts` WHERE `username`=?) AND `group`= (SELECT `group` FROM `student_timetable` WHERE `student_id` = (SELECT `student_id` FROM `student_accounts` WHERE `username`=?)");
    $stmt->bind_param('ss', $inputUser, $inputUser);

这是错误:

  

致命错误:在非对象中调用成员函数bind_param()   第10行/home/u926308913/public_html/get_user_timetable.php

1 个答案:

答案 0 :(得分:0)

由于SQL语句中的错误,$mysqli->prepare函数很可能返回false。

我注意到以下错误:

SELECT * FROM `course_timetable` WHERE 'student_id'=(SELECT 'student_id' FROM `student_accounts` WHERE `username`=?) AND `group`= (SELECT `group` FROM `student_timetable` WHERE `student_id` = (SELECT `student_id` FROM `student_accounts` WHERE `username`=?)
---------------------------------------^----------^---------^----------^

尝试更换'用`

在绑定参数之前,您可以通过执行以下操作来确保prepare语句成功:

$stmt = $mysqli->prepare("SELECT * FROM `course_timetable` WHERE `student_id`=(SELECT `student_id` FROM `student_accounts` WHERE `username`=?) AND `group`= (SELECT `group` FROM `student_timetable` WHERE `student_id` = (SELECT `student_id` FROM `student_accounts` WHERE `username`=?)");

if ($stmt !== FALSE)
{
    $stmt->bind_param('ss', $inputUser, $inputUser);
}
else
{
    echo $mysqli->error; // optional - returns a string description of the last error
}

有关详细信息,请查看文档PHP - mysqli::prepare

此外,如果它有帮助,请阅读JOIN,这可能为您提供更好的方法来检索您想要的记录:Understanding JOINs in MySQL and Other Relational Databases