2010-09-14 27 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