我正在试图找出为什么我的第一个准备声明工作正常,但我的第二个准备工作没有。实际的INSERT INTO语法看起来是正确的,当我替换数字并将其放在mysql控制台中时,它会起作用,但是prepare语句返回false。
只是为了清除简单的问题; $ db_table_prefix ==“uc_”并初始化所有变量。此外,第一个语句设置$ results == 0(编辑:这是我的错误,它真的是0,而不是1)。
global $mysqli,$db_table_prefix;
$stmt = $mysqli->prepare("SELECT COUNT(id) FROM ".$db_table_prefix."attempts WHERE ((exp_m = ?) AND (exp_n = ?) AND (max_base <= ?))");
$stmt->bind_param("iii", $m, $n, $this->max_base);
$stmt->execute();
$stmt->bind_result($results);
$stmt->fetch();
if ($results < 1)
{
$stmt = $mysqli->prepare("INSERT INTO ".$db_table_prefix."attempts (exp_m, exp_n, base_x, max_base) VALUES (?,?,?,?)");
$stmt->bind_param("iiii", $m, $n, $x, $this->max_base);
.....
}
我已经包含了表结构,以防万一就是问题。
mysql> describe uc_attempts;
+----------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------------+------+-----+---------+----------------+
| exp_m | bigint(20) unsigned | NO | | NULL | |
| exp_n | bigint(20) unsigned | NO | | NULL | |
| base_x | bigint(20) unsigned | YES | | NULL | |
| max_base | bigint(20) unsigned | NO | | NULL | |
| id | int(11) | NO | PRI | NULL | auto_increment |
+----------+---------------------+------+-----+---------+----------------+
我确定我只是遗漏了一些简单的东西,但在盯着代码几天后,我需要问一下。在此先感谢您的帮助。如果我应该包含任何其他信息,请告诉我。
答案 0 :(得分:1)
您忘了关闭第一个资源。如果第一个没有结束,我相信它不能打开第二个准备好的声明。我认为$mysqli->prepare( ... );
将返回false,显然false->bind_param( ... );
不存在;-)导致Fatal error: Call to a member function bind_param() on a non-object
- 错误。
global $mysqli,$db_table_prefix;
$stmt = $mysqli->prepare("SELECT COUNT(id) FROM ".$db_table_prefix."attempts WHERE ((exp_m = ?) AND (exp_n = ?) AND (max_base <= ?))");
$stmt->bind_param("iii", $m, $n, $this->max_base);
$stmt->execute();
$stmt->bind_result($results);
$stmt->fetch();
$stmt->close(); //<-- this is the problem
if ($results < 1)
{
$stmt = $mysqli->prepare("INSERT INTO ".$db_table_prefix."attempts (exp_m, exp_n, base_x, max_base) VALUES (?,?,?,?)");
$stmt->bind_param("iiii", $m, $n, $x, $this->max_base);
.....
}