我制作了一个页面,用户可以在其中查看他们在数据库中输入的数据。
我用了:
$select = "SELECT * FROM texts WHERE user='".$user."' ORDER BY date DESC, id DESC";
$result = mysql_query($select);
$array = array();
while($show = mysql_fetch_assoc($result))
{
$array[] = $show;
}
echo "<strong>".$array[0]['id']."</strong><br />";
echo "<strong>".$array[1]['id']."</strong><br />";
echo "<strong>".$array[2]['id']."</strong><br />";
echo "<strong>".$array[3]['id']."</strong><br />";
echo "<strong>".$array[4]['id']."</strong><br />";
代码有效,但有时我要返回的值少于 10 个,有时甚至更多。
如果我使用它并且我只有 2 个数组要返回,我会得到:
Notice: Undefined offset: 2 in ownposts.php on line 15
Notice: Undefined offset: 3 in ownposts.php on line 16
Notice: Undefined offset: 4 in ownposts.php on line 17
仅当存在 $array[4] 时,如何才能回显 $array[4]['id ]?
我试过:
$zero = $array[0];
if(!empty($zero))
{
echo "<strong>".$zero['id']."</strong><br />";
}
$four = $array[4];
if(!empty($four))
{
echo "<strong>".$five['id']."</strong><br />";
}
但不能正常工作,因为我除外并仍然返回Notice: Undefined offsed: 4 in ownposts.php on line 17。