Jump to content

Problem with multiple mysql calls on a static php class


jesbin

Recommended Posts

I've a DataManager class which looks like the following

 

<?php

 

class DataManager {

protected static $link;

private static function _getConnection($dbname = 'wgexpert') {

if (isset(self::$link)) return self::$link;

self::$link = mysql_connect('localhost', 'root', '' );

$db = mysql_select_db('wgexpert') or die('Failure to access the database');

return (self::$link);

}

 

 

public static function getcountries() {

$sql = "call spGetCountries();";

$countries = array();

$result = mysql_query($sql, DataManager::_getConnection());

while ($row = mysql_fetch_array($result)) {

$countries[$row["countryID"]] = $row["countryName"];

}

return ($countries);

}

 

public static function getNominators($name, $org, $country) {

$sql = "call spSearchNominator('$name', '$org', $country);";

$res = mysql_query($sql, DataManager::_getConnection());

while ($row = mysql_fetch_array($res)) {

/ * do something */

}

}

}

?>

 

 

There there is a php page which calls two of the method of the class

 

require_once("classes/class.DataManager.php"); 

 

$data = DataManager::getcountries();

print_r($data);

 

DataManager::getNominators ("b", "", 0);

 

If I call both of then I get the error while calling the second method. If I comment out one of them (any one), it is fine.

I cannot figure out what is going on

Jesbin

The error I get is

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\Work\IPCC\Search.php\classes\class.DataManager.php on line 189

Array ( )

 

This is always the case for the second call. If I switch then around I get it for the second one.

 

The workaround I did was to close the mysql connection, which is not what I wanted to do

 

private static function _getConnection($dbname = 'wgexpert') {

if (isset(self::$link)) {

mysql_close(self::$link); // THIS HAD TO BE DONE

 

}

self::$link = mysql_connect('localhost', 'root', '', true );

$db = mysql_select_db('wgexpert') or die('Failure to access the database');

//echo "cn: " . self::$link . "<br>";

return (self::$link);

}

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.