I spent almost whole evening to understand the following code:

<?php
$link = mysql_connect('localhost', 'root', 'root');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('database_1.1');
if (!$db_selected) {
    die('Could not select database: ' . mysql_error());
}
$query = 'SELECT last_name, first_name FROM Employee';
$result = mysql_query($query);
if (!$result) {
    die('Query failed: ' . mysql_error());
}
/* fetch rows in reverse order */
for ($i = mysql_num_rows($result) - 1; $i >= 0; $i--) {
    if (!mysql_data_seek($result, $i)) {
        echo "Cannot seek to row $i: " . mysql_error() . "\n";
        continue;
    }

    if (!($row = mysql_fetch_assoc($result))) {
        continue;
    }

    echo $row['last_name'] . ' ' . $row['first_name'] . "<br />\n";
}

mysql_free_result($result);
?>

The only vital point of the code I did not understand is,

if (!($row = mysql_fetch_assoc($result)))

Why it has a ! cause whenever we fetch data we do

$row = mysql_fetch_assoc($result))

Please help me to understand the code.Please try to explain a bit.

Thanks to all.God bless you.

Member Avatar for diafol

This looks like a very odd/wasteful piece of code. To get reverse order resultset, you can introduce an ORDER BY clause to the sql. You just need a mysql_num_rows() to see if there are any results.

$query = 'SELECT last_name, first_name FROM Employee ORDER BY id DESC';
$result = mysql_query($query);
if (!$result) {
    die('Query failed: ' . mysql_error());
}
if(mysql_num_rows($result){
   while($row = mysql_fetch_assoc($result)){
      echo $row['last_name'] . ' ' . $row['first_name'] . "<br />\n";
   }
}else{
  echo "No records";
} 
mysql_free_result($result);

Thanks for reply.I can write code better than that but this code is in the example page of php.net.The main reason I am trying to understand this code because I was trying to learn mysql_data_seek() functionality.Please explain the code if any one can.

Member Avatar for diafol

Why? The seek function just moves the internal pointer. If you have the query result set in a particular order already, I can't see the need for it - in this example.

The exclamation point indicates a negative, continue breaks you out of the FOR loop, and $row = mysql_fetch_assoc($result) reads another row from the dataset you're processing with the FOR loop.

So if I'm not misreading it, the code I've copied below is saying:
- read the next row from the dataset $result
- if there's no row left to be read (the result set was empty or all the rows have alreaty been read by the FOR loop), continue with the code after the for loop closes
- if there is a row left to be read, output the name from that record. (The FOR loop will then fire again)

if (!($row = mysql_fetch_assoc($result))) {
  continue;
}
echo $row['last_name'] . ' ' . $row['first_name'] . "<br />\n";

Hope that helps.

(Sometimes we just want to know how something works, even if it's being used improperly.)

commented: I suppose that told me then, didn't it :) +15

Thanks for your explanations. I know the code is not the best way to get reverse output.You are absolutely right scaiferw, I just wanted to know how it is working under the hood.Thanks to all for your precious time and effort.So basically the above code is similarly as follows:

for ($i = mysql_num_rows($result) - 1; $i >= 0; $i--) {
    if (!mysql_data_seek($result, $i)) {
        echo "Cannot seek to row $i: " . mysql_error() . "\n";
        continue;
    }

    if (($row = mysql_fetch_assoc($result))) {
        echo $row['last_name'] . ' ' . $row['first_name'] . "<br />\n";
    }

   
}

Am I correct?

I believe you are correct, although reversing logic like that can on occasion have unanticipated results. Usually the way that makes the most logical sense in your head is the best way to do it, subject to testing to confirm that things are being done right.

One trap with for loops is that a subtle error in logic can cause the first or last record in the dataset ($result in this case) not to be processed, so beware of that.

Hope all that was helpful.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.