Reputation: 313
I've got a strange error from my WAMP (PHP 5.5.12, MySQL 5.6.17).
The main error is: No database selected. I have two database tables here:
cities: id, cities
and
events (some fields are not included here): id, eventHeader, cityID.
So, there is my code. This function displaays all the events, but in the database city is written as cityID, so I have another function that must convert cityID into city name.
public function viewEvents($conf) {
// Connecting to DB with parameters from config file;
$mysqli = $this->dbConnect($conf);
// quering...
$query = "SELECT * FROM events";
$result = $mysqli->query($query);
while($row = mysqli_fetch_array($result)) {
if($row['featured'] == 1) {
$row['header'] = '<b>' . $row['header'] . '</b>';
}
// Getting City Name;
$city = self::getCity($row['id']);
// Echoing table with results here.
echo '';
}
$result->free();
$mysqli->close();
}
This function gets no error at all and works perfect. But the next one...
And this is my getCity($id):
public function getCity($id) {
$conf = $this->getConf(); // Getting config data (with db access);
$mysqli = $this->dbConnect($conf); // connecting to MySQL;
// I'm echoing the possible mysql connection error here;
// Quering...
$query = "SELECT * FROM cities WHERE id = '" . $id . "';";
$result = $mysqli->query($query);
// Echoing mysql query error here with die();
$row = $result->fetch_array();
$city = $row['city'];
return $city;
}
So, this is dbConnect($conf){
public function dbConnect($conf) {
$mysqli = mysqli_connect($conf['db-host'], $conf['db-usr'], $conf['db-psw'], $conf['db-name']);
return $mysqli;
}
Despite of all my code variations I get the same error: No database selected
. Is it possible, cause the first method works perfectly and they both uses the same dbConnect()
?
Upvotes: 1
Views: 535
Reputation: 766
In general it is a good idea to only have one connection during the request lifetime, so this might work for you:
static function dbConnect($conf)
{
static $mysqli = null;
if ( $mysqli === null )
{
$mysqli = mysqli_connect($conf['db-host'], $conf['db-usr'], $conf['db-psw'], $conf['db-name']);
}
return $mysqli;
}
// Call this function like this:
$mysqli = self::dbConnect($conf);
Now, if you have a reliable method that returns the configuration parameters, you could even improve it like this to avoid having to pass the configuration every time. :
static function dbConnect()
{
static $mysqli = null;
if ( $mysqli === null )
{
$conf = $this->getConf();
$mysqli = mysqli_connect($conf['db-host'], $conf['db-usr'], $conf['db-psw'], $conf['db-name']);
}
return $mysqli;
}
// Call this function like this:
$mysqli = self::dbConnect();
This way you will always use only one connection to the database, no matter how many times you call dbConnect(). If the connection is already opened, it will return it; otherwise it will open the connection and then return it.
EDIT: About why the second connection doesn't work
In the viewEvents()
function the call to getCity()
uses the static version self::getCity()
; while inside the getCity()
function there are two calls to object methods: $this->getConf()
and $this->dbConnect()
.
I would suggest to change the call from self::getCity()
to $this->getCity()
inside the viewEvents()
function.
Upvotes: 1