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中創建了所有對象,但沒有問題,但我不喜歡該解決方案。
謝謝!