tags:

views:

10

answers:

1

Hi!

I've been using this solution to get a superglobal mysqli connection:

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

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

//use blst_db::cxn()->prepare(....

I found it here and it was working fine but when I try to get two connections at the same time I get an error. For example, I've a class that runs a query like this one:

$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;  }

I'm creating various objects inside the while statement and that objects constructor runs another query each time (another $query=blst_db::cnx->prepare(...)) and that's the one that is not working and I can't find the problem. If I change the code and create an array inside the while statement and then, after closing that query, I create all the objects inside a foreach I get no problem but I don't like that solution.

Thank you!

+1  A: 

Hi again!

I found the problem. I have to store the results from the first query so I can run the rest inside it. I just added:

$query_points->store_result();

after the execute() call and then I made a free_result() before closing the $query_points and it's working perfectly. I've found the solution here.

Marcos Pérez