Amir Mohammad Rivand Amir Mohammad Rivand - 1 month ago 12
MySQL Question

calling a function from php class

i have witten this php class, but i have problem using it,

<?php
class cs_mysql{
protected $configPath;
protected $db;
function __construct($cP = null){
$this->configPath = $cP;
require $this->configPath;
}
private function connection(){
$db = new mysqli(Config::get('dbHost'),Config::get('dbUser'),Config::get('dbPass'),Config::get('dbName'));
}
public function getRow($table){
$query = 'SELECT * FROM $table ORDER BY `id` DESC';
$sql = $this->db->query($query);
if(!$sql){
echo "FALSE";
}
}
}
?>


i don't know how to run db query :
$sql = $this->db->query($query);

Answer

Change

$db = new mysqli(Config::get('dbHost'),Config::get('dbUser'),Config::get('dbPass'),Config::get('dbName'));

to

$this->db = new mysqli(Config::get('dbHost'),Config::get('dbUser'),Config::get('dbPass'),Config::get('dbName'));

$db is a property of the class. To access class properties (and methods) you have to reference the class instance using $this.