1

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()?

9
  • 1
    Confirm that $conf has the correct values. Commented Sep 22, 2014 at 10:36
  • And you have verified that $conf contains all the expected values in your second function? Commented Sep 22, 2014 at 10:36
  • Sure! Cause it works in the first one. Commented Sep 22, 2014 at 10:37
  • mysqli_query("use <<database>>");, or set the database in the mysqli construct, or use SELECT <<database>>.* Commented Sep 22, 2014 at 10:39
  • Okay, that's what I get: 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 user root Commented Sep 22, 2014 at 10:42

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This solution works perfectly, appreciated! But I am still clueless why the second method does not want to connect :( Maybe I should consider reinstalling WAMP server...
Oh wait! Take a look at the way you call getCity: self::getCity(). This is the static call to a class method, but then inside the method you are using $this->getConf() and $this->dbConnect(). Try changing self::getCity() for $this->getCity()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.