0

I have this code:

class database{
    var $conn;
    function connect($server,$host_username,$host_password,$host_database){
        $server = 'localhost';
        $host_username = 'root';
        $host_password = '';
        $host_database = 'e-vent system db';

        $conn= new mysqli($server,$host_username,$host_password,$host_database);

        if ($conn->connect_error){
            die("db connection error:". $conn->connect_error);
        }
    }

    function read_db($table,$condition){
        $read="SELECT * FROM ".$table." ".$condition;
        $read_result=$conn->query($read);
        if(!$read_result){
            echo "select error:". mysqli_error($conn);
        }
        return $result;
    }
}

And I get this error:

Notice: Undefined variable: conn in C:\xampp\htdocs\E-vent\database.php on line 19

How can I make the $conn variable visible to the read_db function?

yivi
  • 42,438
  • 18
  • 116
  • 138
Vic
  • 29
  • 6

4 Answers4

3

Change this line:

if ($conn->connect_error){

to

if ($this->conn->connect_error){

means replace all:

$conn->query

to

$this->conn->query

Explanation: If you want to use a variable in the entire class scope then you have to use the class variable instead of local (function) scope variable. In this line:

$conn->connect_error

the $conn is a local variable whose scope is limited to the function only, but when you use $this->conn, it means you are referring to the class variable which is accessible in all the member functions of the class.

And put all the content of connect function in the class constructor so that this connection is initialized at the time of class initialization. (Thanks @Magnus for pointing this)

Have a look on the variable scope, it will help you to understand the concept.

Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59
1

You are using class variable into a function, it's basic rule of oops that to use class variable we must access it using object. so class variable can be used under same class using $this.

so your code must be:

$this->$conn->connect_error

Instead of

$conn->connect_error
Qirel
  • 25,449
  • 7
  • 45
  • 62
Chintan Trivedi
  • 207
  • 1
  • 5
0

You need to make the connection object a property of the class, using $this - specifically, $this->conn. Assign your connection-object to that property. You then need to reference $this->conn everywhere else within that class, instead of using $conn.

class database {
    public $conn;

    public function connect($server, $host_username, $host_password, $host_database)
    {
        $server = 'localhost';
        $host_username = 'root';
        $host_password = '';
        $host_database = 'e-vent system db';

        $this->conn = new mysqli($server, $host_username, $host_password, $host_database);

        if ($this->conn->connect_error) {
            die("db connection error:". $this->conn->connect_error);
        }
    }

    public function read_db($table, $condition)
    {
        $read = "SELECT * FROM ".$table." ".$condition;
        $read_result = $this->conn->query($read);
        if (!$read_result) {
            echo "select error:". mysqli_error($this->conn);
        }
        return $result;
    }
}

That being said, a couple of things to note,

  1. You are not using parameterized queries, and are injecting variables directly into the query -- this is not secure, and you should use a prepared statement with placeholders.
  2. The connect() method takes in all the arguments, but you still overwrite them in the function. Alternatives are to remove the arguments, remove the hard-coded values, or use the hard-coded values if the argument is empty.
  3. You should not return errors to the user while in production. During development, this is fine - but in production, hide them, log them and display something generic to the user instead (like a 500 page, or some other error page).
  4. You should specify if your properties and methods are public, protected, static or private.
Qirel
  • 25,449
  • 7
  • 45
  • 62
0

Better to use public access modifier without using var. What does PHP keyword 'var' do?.

Class properties must be defined as public, private, or protected. If declared using var, the property will be defined as public.

And also The PHP 4 method of declaring a variable with the var keyword is still supported for compatibility reasons (as a synonym for the public keyword). In PHP 5 before 5.1.3, its usage would generate an E_STRICT warning.

PHP variables - http://php.net/manual/en/language.variables.variable.php

Need to change as follows

class database{
    public $conn

And here

$this->conn= new mysqli($server,$host_username,$host_password,$host_database);

And here

if ($this->conn->connect_error){
    die("db connection error:". $this->conn->connect_error);
}

And here as well

$read_result=$this->conn->query($read);
cha
  • 730
  • 2
  • 12
  • 28