php mysqli“没有数据库选择”使用类

When I'm using mysqli without class it's going ok:

index.php

require_once(dirname(__FILE__) . '/config.php');

$mysqli = new mysqli($hostname, $username, $password, $dbname);
$queryText = "SELECT * FROM User";
if($query = $mysqli->query($queryText)) {
    $results = $query->fetch_array();
    echo $results['userId'];
} else {
     echo "Error ";
     echo $mysqli->errno . " " . $this->mysqli->error;
}

?>

But when I start using mysqli with class something goes wrong. connectDB doesn't give any error, so i get connected to DB. But then when trying do any query it give me "No database selected error" Result of index.php is: Error 1046 No database selected

index.php

<?php
require_once(dirname(__FILE__) . '/banana.php');

$banana = new Banana(1);
if ($banana->connectDB()) {
    $banana->doQuery();
}
?>

banana.php

<?php

require_once(dirname(__FILE__) . '/config.php');

class Banana {

     private $mysqli, $userId, $query;

     function __construct($userId) {
          $this->userId = $userId;
     }

     function __destruct() {
        $this->mysqli->close();
     }

     public function connectDB() { // Подключение к БД
          $this->mysqli = new mysqli($hostname, $username, $password, $dbname);
          if ($this->mysqli->connect_errno) {
               echo "Error (" . $this->mysqli->connect_errno . ") " . $this->mysqli->connect_error;
               return false;
          }
          return true;
     }

     public function doQuery() {
        $queryText = "SELECT * FROM User";
        if($this->query = $this->mysqli->query($queryText)) {
            $results = $query->fetch_array();
            echo $results['userId'];
        } else {
            echo "Error ";
            echo $this->mysqli->errno . " " . $this->mysqli->error;
        }
    }

?>

So it's very frustrating. I'm about 2 weeks in php, but can't find answer for couple days. I guess the answer is obvious but I can't see it. Thank you for your time and patience.

One of the first problems you will encounter when you run your script is here:

 public function connectDB() { // Подключение к БД
      $this->mysqli = new mysqli($hostname, $username, $password, $dbname);

Note that all 4 variables you are using in your function call ($hostname, etc.) are undefined in the scope of the method.

There are several ways you can solve this:

  • Pass the variables as parameters to the method:
    public function connectDB($hostname, ...
  • Pass the necessary variable to your class constructor and set configuration properties in your class that you can use later on;
  • Use constants instead of variables;
  • Declare your variables global.

I would recommend one of the first 2 and definitely not the last one.

You can read more in the php manual about variable scope.

It looks like you are a copy'n'paste victim. In doQuery() change:

if($this->query = $this->mysqli->query($queryText)) {
        $results = $query->fetch_array();

To:

if($this->query = $this->mysqli->query($queryText)) {
        $results = $this->query->fetch_array();