我的连接类; firstcode.php
class DB_functions {
public $db;
function __construct() {
try{
$db = new PDO("mysql:localhost;dbname=xxx;charset=utf8","xxx","xxx");
echo 'Connected';
}catch(PDOException $e){
print $e->getMessage();
echo "No Connection";
}
}
function __destruct() {}
public function test(){
$query = $db->query("SELECT * FROM User", PDO::FETCH_ASSOC);
if($query->rowCount()){
foreach ($query as $row) {
print_r($row);
}
}
}
}
我的测试PHP文件;
require_once('firstcode.php');
$db = new db_functions();
$t = $db->test();
?>
我得到的错误;
Notice: Undefined variable: db in firstcode.php on line 20
Fatal error: Call to a member function query() on a non-object in firstcode.php on line 20
提前致谢