这是我目前的PHP代码:
$sql = 'SELECT * from comments where post_id_fk=$post_id';
$users = $db->prepare($sql);
$users->execute();
while($row = pg_fetch_array($users, 0, PGSQL_ASSOC))
出于某种原因,我不断收到以下错误:
警告:pg_fetch_array()期望参数1是资源,第39行/home/se212004/public_html/content.html中给出的对象
行号是指while
循环的行。我曾尝试多次修复它,但我无法让它工作。
答案 0 :(得分:1)
您正在使用PDO准备和执行语句,我相信您也应该使用它来获取结果。所以,基本上,你必须做这样的事情:
$sql = 'SELECT * from comments where post_id_fk=$post_id';
$users = $db->prepare($sql);
$users->execute();
$results = $users->fetchAll();
或者你可以这样做:
$sql = 'SELECT * from comments where post_id_fk=$post_id';
$users = $db->prepare($sql);
$users->execute();
while ($row = $users->fetch(PDO::FETCH_ASSOC)){
// do something with each row
}