I am having some trouble to get database query using below code. I am first time using classes and even PDO for connection so no idea what is wrong. For you knowledge I am not so master but trying to learn class so I would appreciate if you can tell me if anything can improve for this too.
Error Message: Fatal error: Call to a member function query() on a non-object in C:\ pat \ to\ class.php on line 72
<?php
// get database connection
private static function _db_connect() {
try {
$db_con = new PDO('mysql:host='.$db_host.';dbname='.$db_name, $db_user, $db_pass);
$db_con = null;
} catch (PDOException $e) {
print "Error!" . $e->getMessage(). "<br/>";
}
}
// get database tables prefix
public static function prefix() {
$prefix = 'tb_'; // once done will be set dynamically
return $prefix;
}
// get all users from db
public static function get_users() {
$db = self::_db_connect();
$prf = self::prefix();
$st = $db->query('SELECT * FROM '.$prf.'users'); // this is the line #72 where error
while($row = $st->fetch(PDO::FETCH_ASSOC))
$rs[] = $row;
return count($rs) ? $rs : array();
}
?>
EDIT: I have remove null and return PDO object here
// get database connection
private static function _db_connect() {
try {
$db_con = new PDO('mysql:host='.$db_host.';dbname='.$db_name, $db_user, $db_pass);
return $db_con;
} catch (PDOException $e) {
print "Error!" . $e->getMessage(). "<br/>";
}
}
// get database tables prefix
public static function prefix() {
$prefix = 'tb_'; // once done will be set dynamically
return $prefix;
}
// get all users from db
public static function get_users() {
$db = self::_db_connect();
$prf = self::prefix();
$st = $db->query('SELECT * FROM '.$prf.'users'); // this is the line #72 where error
while($row = $st->fetch(PDO::FETCH_ASSOC))
$rs[] = $row;
return count($rs) ? $rs : array();
}
?>