2010-09-14 44 views
0

不止一个方面,我一直在使用这种解决方案来获得超全局的mysqli连接:mysqli的超全局连接对象,在时间

class blst_db { 
private static $mysqli; 
private function __construct(){} //no instantiation 

static function cxn() { 
    if(!self::$mysqli) { 
     self::$mysqli = new mysqli(...); 
    } 
    return self::$mysqli; 
}   

//使用 blst_db :: CXN() - >准备(。 ...

我发现它here它工作的很好,但是当我尝试在同一时间获得两个连接时出现错误例如,我有一个运行如下查询的类:

$query_points = blst_db::cnx()->prepare('SELECT point_id FROM points WHERE id=?'); 
$query_points->bind_param('i', $this->id); 
$query_points->bind_result($point_id); 
$query_points->execute(); 
while ($query_points->fetch()) { 
    $point = new blst_point ($point_id); 
    $points[] = $point; } 

我在while语句中创建了各种对象,并且对象构造函数每次都运行另一个查询(另一个$ query = blst_db :: cnx-> prepare(...)),那是不工作的,我可以找不到问题。如果我更改代码并在while语句内创建一个数组,然后在关闭该查询之后,我在foreach中创建了所有对象,但没有问题,但我不喜欢该解决方案。

谢谢!

回答

1

我发现了这个问题。我必须存储来自第一个查询的结果,以便我可以在其中运行其余部分。我只是说:

$query_points->store_result(); 
的execute()调用后

,然后我关闭$ query_points,它的正常使用之前创建的free_result()。 我找到了解决方案here