我试图在 PHP 中建立一个 mysqli DB 连接。问题是每次我尝试获取结果时,都会执行数千个查询,并且 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 "
数据库对象:
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 无法决定何时停止发送要获取的查询......
我究竟做错了什么?有人能帮我吗?