0

我试图弄清楚为什么我的第一个准备语句工作得很好,但我的第二个没有。当我替换数字并将其放在 mysql 控制台中时,实际的 INSERT INTO 语法看起来正确且有效,但准备语句返回 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 |
+----------+---------------------+------+-----+---------+----------------+

我确定我只是错过了一些简单的东西,但是在盯着代码几天之后,我需要问一下。在此先感谢您的帮助。如果我应该包括任何其他信息,请告诉我。

4

1 回答 1

1

您忘记关闭第一个资源。我相信如果第一个未关闭,它就无法打开第二个准备好的语句。我认为$mysqli->prepare( ... );然后会返回 false 并且显然false->bind_param( ... );不存在 ;-) 导致您的Fatal error: Call to a member function bind_param() on a non-object-error。

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);

  .....
}
于 2013-06-28T16:58:12.080 回答