我在脚本中运行两个查询room.php
。两者都使用MySQLi准备好的语句,其代码如下:
/* Get room name */
$stmt = $mysqli->prepare('SELECT name FROM `rooms` WHERE r_id=?');
$stmt->bind_param('i', $roomID);
$stmt->execute();
$stmt->bind_result($roomName)
/* Add this user to the room */
$stmt = $mysqli->prepare('INSERT INTO `room_users` (r_id, u_id) VALUES (?, ?)');
$stmt->bind_param('ii', $roomID, $_SESSION['userID']);
$stmt->execute();
当我运行脚本时,我收到此错误:
Fatal error: Call to a member function bind_param() on a non-object in C:\wamp\www\room.php on line 24
这是第二个查询。如果我从脚本中删除第一个查询,一切都会正常运行。同样,如果我删除第二个查询。这让我相信存在问题,因为我正在重用$stmt
目的。如果我尝试使用第二个查询$stmt2
我仍然收到错误。
我的所有数据库表和字段都存在,因此查询没有任何问题。
所有 mysqli 函数/方法都可能失败,在这种情况下它们将返回 false。 IE。如果prepare()失败,$stmt不是一个可以调用方法的对象,而是一个bool(false)。您必须检查返回值并添加一些错误处理,例如
$stmt = $mysqli->prepare('SELECT name FROM `rooms` WHERE r_id=?');
if ( !$stmt ) {
printf('errno: %d, error: %s', $mysqli->errno, $mysqli->error);
die;
}
$b = $stmt->bind_param('i', $roomID);
if ( !$b ) {
printf('errno: %d, error: %s', $stmt->errno, $stmt->error);
}
$b = $stmt->execute();
if ( !$b ) {
and so on and on
see http://docs.php.net/mysqli-stmt.errno http://docs.php.net/mysqli-stmt.errno et al
在这种情况下,您可能会遇到这样的问题:当上一个语句仍有待处理的结果/结果集时,您无法创建其他语句。
see http://docs.php.net/mysqli-stmt.close http://docs.php.net/mysqli-stmt.close:
Closes a prepared statement. mysqli_stmt_close() also deallocates the statement handle. If the current statement has pending or unread results, this function cancels them so that the next query can be executed.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)