I made a page where users can see data entered by them in the database.
I used:
$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 />";
Code works, but sometimes I have less than 10 values to return, sometimes even more.
If I use this and I have only 2 arrays to return, I get:
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
How it's possible to echo $arrray[4]['id] only if exist $array[4]?
I've tried with:
$zero = $array[0];
if(!empty($zero))
{
echo "<strong>".$zero['id']."</strong><br />";
}
$four = $array[4];
if(!empty($four))
{
echo "<strong>".$five['id']."</strong><br />";
}
But doesn't work as I excepted and still return Notice: Undefined offsed: 4 in ownposts.php on line 17.
mysql_*functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.echoinsidewhile.