我为 PHP 制作了一个单例数据库类。我认为它工作得很好,但实际上并非如此。我现在制作一个有 3 个查询的页面。1检查相册是否存在,1检查用户是否拥有相册,另一个人从相册中获取照片。
现在在我的第三个查询中,我填充了一个对象,但是第一个 2 个查询的结果也在该数组中,所以我得到了通知!
这是一个例子:
Array
(
[0] => Array
(
[id] => 2
[name] => My new album 1
[slug] => my-new-album-1
[user_id] => 1
[views] => 0
[datecreated] => 2013/03/23 16:00:43
)
[1] => Array
(
[id] => 3
[name] => My new album 1
[slug] => my-new-album-1
[user_id] => 1
[views] => 0
[datecreated] => 2013/03/23 23:51:58
)
[2] => Array
(
[id] => 2
)
[3] => Array
(
[id] => 117
[title] =>
[location_id] =>
[date] => 2013-03-30 00:42:26
[user_id] => 1
[album_id] => 2
)
这就是我进行查询并返回数组的方式:
mysqli_conn::getInstance()->query($sql)->all_assoc()
这是我的数据库类的一部分,它执行查询并返回结果:
public function query( $sql ){
$starttime = $this->time_to_float();
$this->query = mysqli_query($this->connection, $sql);
$endtime = $this->time_to_float();
$exectime = ($endtime - $starttime);
if (!$this->query){
throw new Exception(mysqli_error($this->connection));
} else {
$this->arQueryLog[] = array ( 'query' => $sql,
'exectime' => $exectime,
'affected_rows' => mysqli_affected_rows($this->connection),
'last_insert_id' => $this->lastID() );
}
return $this;
}
public function all_assoc ()
{
while($result = mysqli_fetch_assoc($this->query)){
$this->result[] = $result;
}
return $this->result;
}
怎么可能只有最后一个查询结果在结果数组中?
谢谢!!