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.