-3

Only the part which is $this->link->query($query) I understand that link is the member variable of the class Database but don't understand what is happening when $this->link->query($query) executes? help, I am a novice at coding

My whole code:

<?php


class Database
{
    public $db_host=DB_HOST;
    public $db_user=DB_USER;
    public $db_pass=DB_PASS;
    public $db_name=DB_NAME;


    public $link;

    public $error;

    public function __construct()

    {



        // Call connect function

        $this->connect();


    }

    private function connect()
    {
        $this->link= new mysqli($this->db_host,$this->db_user,$this->db_pass,$this->db_name);

        if(!$this->link)
        {
            $this->error="Connection Failed";
            return false;
        }

    }

    public function select($query)
    {
        $result=$this->link->query($query) or die ("Query could not execute");
    }

}


?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Pranav Raj
  • 13
  • 1

2 Answers2

0
  1. $this is the current Database object you are using.
  2. -> calls a method or member of that object
  3. link as you pointed out is a member of Database which just happens to be another object of mysqli.
  4. query($query) is a method of link.

So you are calling mysqli query. Where the param $query should be your sql

Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
nerdlyist
  • 2,842
  • 2
  • 20
  • 32
0

$this->link is a database connection object, and it has a method called query() for doing database queries and returning a mysqli result object. The query() method takes an SQL string. So one way to invoke it would be:

$result = $this->link->query("SELECT * FROM table WHERE 1") ;

It's very common to use a variable instead of a string literal in calls to mysqli query():

$sql = "SELECT * FROM table WHERE 1" ;
$result = $this->link->query($sql) ;

In the case of your code, the variable containing the SQL string is called $query, which can be kind of confusing, especially since $query isn't defined anywhere that I can see.

Juan Tomas
  • 4,905
  • 3
  • 14
  • 19