以下のコードを使用してデータベースクエリを取得するのに問題があります。クラスや接続用の PDO を初めて使用するので、何が問題なのかわかりません。ご存知のように、私はそれほどマスターではありませんが、クラスを学ぼうとしているので、これについても改善できることがあれば教えていただければ幸いです.
エラー メッセージ: 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();
}
?>
編集: null を削除し、ここで PDO オブジェクトを返します
// 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();
}
?>