PHP mysqli在fetch()上执行数千个查询

时间:2013-11-08 13:22:27

标签: php mysql mysqli

我正在尝试在PHP中构建一个mysqli数据库连接。问题是我每次尝试获取结果时,实际上会执行数千个查询,并且MySQL服务器和/或浏览器会死掉。

以下是我使用的代码:

    $resultQuery = $this->db->prepare($query_id);
    var_dump($this->db);
    var_dump($resultQuery);

    $resultQuery->execute();

    //$result = $resultQuery->dbStatement->fetch();
    $resultQuery->close();

    return $result;

如果我使用fetch方法取消注释该行,一切都很疯狂......

以下是上述代码中使用的var_dumb的结果:

查询:

string(69) "   SELECT   userid, fname, lname   FROM   users   WHERE   finished=0 "

db object:

object(mysqli)#2 (17) {
  ["affected_rows"]=>
  int(-1)
  ["client_info"]=>
  string(6) "5.1.49"
  ["client_version"]=>
  int(50149)
  ["connect_errno"]=>
  int(0)
  ["connect_error"]=>
  NULL
  ["errno"]=>
  int(0)
  ["error"]=>
  string(0) ""
  ["field_count"]=>
  int(0)
  ["host_info"]=>
  string(25) "Localhost via UNIX socket"
  ["info"]=>
  NULL
  ["insert_id"]=>
  int(0)
  ["server_info"]=>
  string(17) "5.1.66-0+squeeze1"
  ["server_version"]=>
  int(50166)
  ["sqlstate"]=>
  string(5) "00000"
  ["protocol_version"]=>
  int(10)
  ["thread_id"]=>
  int(2220)
  ["warning_count"]=>
  int(0)
}
在fetch()之前的

响应:

object(mysqli_stmt)#47 (9) {
  ["affected_rows"]=>
  int(0)
  ["insert_id"]=>
  int(0)
  ["num_rows"]=>
  int(0)
  ["param_count"]=>
  int(0)
  ["field_count"]=>
  int(3)
  ["errno"]=>
  int(0)
  ["error"]=>
  string(0) ""
  ["sqlstate"]=>
  string(5) "00000"
  ["id"]=>
  int(1)
}

以下是我连接数据库的方式:

    if ($this->persistency)
    {
    $this->db = new mysqli("p:" . $this->server, $this->user, $this->password);
    }
    else
    {
    $this->db = new mysqli($this->server, $this->user, $this->password);
    }

    $this->db->query("SET CHARACTER SET utf8") or die("Error during character set initialization | " . mysql_error());
    $this->db->query("SET NAMES 'utf8'") or die("Error during character set names initialization | " . mysql_error());

    if (!$this->db->connect_error)
    {
    if ($database != "")
    {
        $this->dbname = $database;
        $dbselect = $this->db->select_db($this->dbname);

        if (!$dbselect)
        {
        $this->db->close($this->db);
        $this->dbError = "Error choosing database!";
        }
        else
        {
        $this->dbResults = new mysqli_result();
        $this->dbStatement = new mysqli_stmt();
        return $this->dbError = false;
        }
    }
    }
    else
    {
    return $this->dbError = $this->db->connect_error;
    }

似乎mysqli无法决定何时停止发送要获取的查询...

我做错了什么?有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

您是否将结果绑定到var?您正在使用mysqli :: prepare但我没有看到选择绑定到任何变量的结果:

http://php.net/manual/en/mysqli-stmt.bind-result.php