1
$sql = mysql_query("SELECT * FROM table WHERE user='$user'" ); 

while($data=mysql_fetch_array($sql)){
    echo $data['user']; //this is fine

}

echo $data['user'];
$data=mysql_fetch_array($sql);
echo $data['user'];

The last two echos are empty?

I need to have an array outside of the loop that is equivalent to the 'last' loop cycle.

13
  • the last 2 echos are empty : this is because .. the variable definition of $data only belongs to wihle loop. and outside it the loop it won't be accessible. its a general concept Commented Nov 2, 2013 at 15:27
  • on the second example I redeclare $data Commented Nov 2, 2013 at 15:29
  • Um... I don't see a second example? Commented Nov 2, 2013 at 15:31
  • @NullPoiиteяღ PHP doesn't use block scoping, it uses function and class scoping. Commented Nov 2, 2013 at 15:31
  • The last mysql_fetch_array() returns false because there are no more rows to fetch -- the loop fetched everything. Commented Nov 2, 2013 at 15:33

2 Answers 2

2

The while loop keeps fetching data, until there is no more data and therefore false is returned by mysql_fetch_array. This last value, false, is still in $data after the loop has ended, but simply printing it using echo, won't print anything you can see. The same goes for the next call of mysql_fetch_array and echo. You can check this by doing a var_dump of the $data variable. This will show you that it contains the boolean false.

If you want to be able to use the data after the loop has ended, you can save all the data you fetch into one big array. That might be a bad option though if you're fetching a lot of data, but from there you should be able to change it yourself so you can save and use the useful data. To store all the data in an array, change the loop into this:

$alldata = array();
while($data=mysql_fetch_array($sql))
{
  echo $data['user'];
  $alldata[] = $data;
}

You can then for example iterate over $alldata to go over the results again.

Update: In your last comment you said you want to access the last record that was fetched. You can do that like this:

$lastdata = array();
while($data=mysql_fetch_array($sql))
{
  echo $data['user'];
  $lastdata = $data;
}

$lastdata will then contain the last record in your result.

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

Comments

2

Because of the following:

while($data = mysql_fetch_array($sql)) {
    // mysql_fetch_array returned something 'not false' and this value
    // is assigned to $daat
}
// mysql_fetch_array() returned false, because there are no more rows
// false is assigned to $data, and because the statement within while(...)
// isn't true anymore the loop is stopped.

Use

$data = mysql_fetch_array($sql);
if (!$data) {
    echo "User don't exists";
}

for example to handle this situation

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.