-1

I am not so good in PHP oops. Here for learning purpose, I am trying to make an mysql class. the code for mysql class is.

  Class Db {
        public $mysqlCon; 

        function  __construct() {

               $mysqlCon = mysqli_connect(_DBHOST, _DBUSER, _DBPASSWORD, _DBNAME)
                           || die(mysqli_connect_error());
               return $mysqlCon;

        }

        function query($query) {

                $result1 = mysqli_query($mysqlCon, $query) 
                           or die(mysqli_error($mysqlCon));
                $result2 = mysqli_fetch_array($result1, MYSQLI_NUM);
                print_r($result2);
                exit;
         }
}

Then another php page, I am calling the Db clas as following.

require_once _BASEPATH.'class/Db.php';

$db = new Db;
$db->query('SELECT * FROM profiles');

But here I am not getting any error during mysql connection. But when I am calling the query method of Db class, I am getting following error.

Notice: Undefined variable: mysqlCon in /opt/lampp/htdocs/qb/class/Db.php on line 16

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in    /opt/lampp/htdocs/qb/class/Db.php on line 16

Notice: Undefined variable: mysqlCon in /opt/lampp/htdocs/qb/class/Db.php on line 17

Warning: mysqli_error() expects parameter 1 to be mysqli, null given in /opt/lampp/htdocs/qb/class/Db.php on line 17

So I think the issue is mysqlCon variable. How to fix this. Also I do not want to connect to databse for each query.

Please Help me.

Note : If you think there is any error in my question, feel free to edit.

Dinesh Patra
  • 1,125
  • 12
  • 23

1 Answers1

0

You have to refer to the variable inside the class with $this->! In your example use $this->mysqlCon always:

Class Db {
        public $mysqlCon;

        function  __construct() {
               $this->mysqlCon = mysqli_connect(_DBHOST, _DBUSER, _DBPASSWORD, _DBNAME);
               if( !$this->mysqlCon ) die(mysqli_connect_error());
               return $this->mysqlCon;
        }

        function query($query) {

                $result1 = mysqli_query($this->mysqlCon, $query);
                if( !$result1 ) die(mysqli_error($this->mysqlCon));

                $result2 = mysqli_fetch_array($result1, MYSQLI_NUM);
                print_r($result2);

         }
}

Look into the documentation to learn more about it: http://php.net/manual/en/language.oop5.basic.php#example-188 - you should read the whole page to get started.

The second or was:

$mysqlCon = mysqli_connect(_DBHOST, _DBUSER, _DBPASSWORD, _DBNAME) or die(mysqli_connect_error());
# --> mysqlCon will be a bool

If you do $a = $x || $y then $a will always be a boolean and not $x or $y - that's simply how php works. Thats why you have to do $a = $x; if( !$a ) $y; for what you mean.

Markus
  • 5,667
  • 4
  • 48
  • 64
  • Hello, still no solution, I am getting the error. Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in /opt/lampp/htdocs/qb/class/Db.php on line 16 Warning: mysqli_error() expects parameter 1 to be mysqli, boolean given in /opt/lampp/htdocs/qb/class/Db.php on line 17 – Dinesh Patra Dec 25 '14 at 23:55