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中创建了所有对象,但没有问题,但我不喜欢该解决方案。
谢谢!