0

After procedural php I study the basic of OOP since last week and tried to connect database. But it shows me unexpected error:

unexpected '$conn' (T_VARIABLE), expecting function (T_FUNCTION)

PHP code:

class Database 
{
   public $conn;

   private $host = "localhost";
   private $user = "root";
   private $pass = "";
   private $db   = "inventory";

   // Create connection
    $conn = new mysqli($host, $user, $pass, $db);

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    echo "Connected successfully";
}

3 Answers 3

1

Try this :

<?php

class Database {
   public $conn;

   private $host = "localhost";
   private $user = "root";
   private $pass = "";
   private $db   = "inventory";

   public function __construct()
   {
       // Create connection
       $this->conn = new mysqli($this->host, $this->user, $this->pass, $this->db);

        // Check connection
        if ($this->conn->connect_error) {
            die("Connection failed: " . $this->conn->connect_error);
        }
        echo "Connected successfully";
    }

    public function getCon() {
        return $this->conn;
    }

}

?>

You need a constructor. For use your class just :

$oDatabase = new Database();

You need getter for acces at $conn;

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

Comments

0

You have to store your connection code into a function. Dont forget to remove public $conn; from the top.

  public function createConnection ()
  {
       // Create connection
       $conn = new mysqli($this->host, $this->user, $this->pass, $this->db);

       // Check connection
       if ($conn->connect_error) {
           die("Connection failed: " . $conn->connect_error);
       }

       //echo "Connected successfully";
       return $conn;
   }

Then u can create an object of the class.

  $db = new Database(); // Your main object
  $conn = $db->createConnection(); // The connection object

Note that i've been using $this->variable to access your private variables in your class.

Now you can use the connection object. e.g. $conn->query("SELECT * FROM MyTable");

2 Comments

Howells, it solved my problem, can I use $this->conn as mysqli instance to use as global instance variable and private/protected visibility with createConnection() method.
I've edited my post, now you get the connection object in return if you create the connection, which you can use for queries.
0

Your class doesn't have any function. You need, for example, a connect() to connect to the database.

    class Database {
      // public $conn;

       private $host = "localhost";
       private $user = "root";
       private $pass = "";
       private $db   = "inventory";

       // Create connection
       function connect() {
         $conn = new mysqli($host, $user, $pass,$db);
          // Check connection
          if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
          }
          echo "Connected successfully";
       }
    }

Comments

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.