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()?
$confhas the correct values.$confcontains all the expected values in your second function?mysqli_query("use <<database>>");, or set the database in themysqliconstruct, or useSELECT <<database>>.*SELECT command denied to user ''@'localhost' for table 'cities'. User described in config file has grant access to this database... The same happens while using userroot