1

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.

8
  • 1
    Please, don't use 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. Commented Sep 4, 2013 at 11:08
  • Just move echo inside while. Commented Sep 4, 2013 at 11:09
  • Try not to use 'mysql_' functions anymore, it's deprecated and should not be used in new applications. Instead use Mysqli or PDO Commented Sep 4, 2013 at 11:09
  • Still don't work if I move echo inside while. Commented Sep 4, 2013 at 11:11
  • Deprecated bla bla bla... There's absolutly no point wasting cpu time in preparing a statement that will only be ran once, and deleted on request end, as long as you properly escape input using mysql_real_escape_string. Commented Sep 4, 2013 at 11:15

4 Answers 4

3

Instead of what you currently do, this:

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 />";

Why not just show whatever is found by mysql as you find it:

while($show = mysql_fetch_assoc($result))
  {
    echo "<strong>".$show['id']."</strong><br />";
  }

Or if you have other things to do with the array than the code you showed us, use a loop on the newly created array, ie a foreach.

Sign up to request clarification or add additional context in comments.

Comments

1

try

foreach($array as $data){
echo '<strong>'.$data['id'].'</strong><br />';
}

Comments

0

I'd rewrite it as follows:

$i = 0;
while($show = mysql_fetch_assoc($result))
{
    $array[] = $show;
    if (isset($array[$i])) {
        echo "<strong>".$array[$i]['id']."</strong><br />";
    }
    $i++;
}

Comments

0

there is function in php for that to check, array_key_exists. You can check that at, http://php.net/manual/en/function.array-key-exists.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.