我得到一个致命的错误上执行下面的查询:为什么此查询脚本返回致命错误?
$stmt = $db->query('SELECT * FROM comments LIMIT 50');
while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo $result['name'] . ':' . strip_tags($result['message']);
}
返回:
Fatal error: Call to a member function query() on a non-object
设置我的$ DB是这样的:
try {$db = new PDO('mysql:host=localhost;dbname=database', 'username', 'password');}
catch(PDOException $e) {echo $e->getMessage();}
有谁知道是什么原因导致这错误?
你忘记了初始化$ db吗? –
你是如何设定$ db的? – Dogbert