未定义的变量 在 foreach 上 ?警告:为 foreach() 提供的参数无效


undefined variable on foreach ? Warning: Invalid argument supplied for foreach()

<?php
include 'post.php';
include 'db.php';
mysqli_select_db($pdo, 'ocp') or die('no db connection');
exit();
$q = 'SELECT * FROM posttbl ';
$res = mysqli_query($pdo, $q);
foreach($res as $row){
    $sells[]=array('postid'=>$row['postid'],'title'=>$row['title'],'desc'=>$row['desc']);
}
?>

错误是"未定义的变量:第 12 行的'卖出'..我错过了什么吗?我似乎找不到它..还是我使用每种权利?这段代码实际上在学校工作,然后当我在家里尝试时,错误突然出现。所有错误都在第 12 行中,即

对不起,伙计们重新检查了问题,它在我的另一个页面帖子上.php

<html>
<body>
<h3>SELLING</h3>
<table border="1" width="50%">
<thead>
<tr>
<th>Title</th>
</tr>
</thead>
<tbody>
<?php  foreach ($sells as $sell) : ?>  <----------*this is supposed to be the problem
<tr>
<td><?php echo $sell ['title']; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</body>
</html>

我将把"array();"放在foreach旁边吗? 也很抱歉我只是将其用作变量$pdo。我使用MySQLI作为我的数据库...抱歉,我习惯使用$pdo作为我的变量数据库,所以很抱歉这是一个坏习惯。

include 'post.php';只是将文件内容附加到该位置。

因此,您正在尝试访问稍后实际定义的变量$sells。您首先需要填充变量,然后访问它。