I am having trouble using a mysqli object that was instantiated within one method of my class in a second method of the class. Can anyone help me to understand what I might be doing wrong? Here is what my two class methods look like:

```
function connect() {
    $mysqli = new mysqli($this->host, $this->user, $this->pass, $this->db);

    if($mysqli->connect_errno) {
        printf("Connect failed: %s\n", $mysqli->connect_error);
        exit();
    }
    else {
        echo 'Successful';
    }

}

public function query($sql) {
    $this->sql = $sql;
    $mysqli->connect();
    $result = $mysqli->query($this->sql);

    while ($row = $result->fetch_assoc()) {
        printf ("$s (%s)\n", $row['name']);
        echo "<br>";
    }

    $result->free();

    $mysqli->close();
}

```

I think my trouble is making the instantiated object public.

\*\*\*\*UPDATED: This is currently the entire class that I have and the script is dying when trying to execute a line within the query method:

```
$this->mysqli->connect();

```

---

```
class database {
    public $host = "host";
    public $user = "user";
    public $pass = "pw";
    public $db = "dbname";
    public $sql;
    private $mysqli;

    function __construct() {
        echo "new class object created";
        echo "<br><br>";
    }

    function connect() {
        $this->mysqli = new mysqli($this->host, $this->user, $this->pass, $this->db);

        if($this->mysqli->connect_errno) {
            printf("Connect failed: %s\n", $mysqli->connect_error);
            exit();
        }
        else {
            echo 'Successful';
        }

    }

    public function query($sql) {
        $this->sql = $sql;
        echo "test1";
        $this->mysqli->connect();
        echo "test2";
        $result = $this->mysqli->query($this->sql);
        echo "test3";

        while ($row = $result->fetch_assoc()) {
            printf ("$s (%s)\n", $row['name']);
            echo "<br>";
        }

        $result->free();

        $this->mysqli->close();
    }
}

```

---

```
echo "test2"; does not execute.

```