Jump to content

Shortcut PHP?


pandu

Recommended Posts

Hi,

 

I am getting confused by what seems like some kind of shortcut version of PHP, especially when using loops. For example, in the code below, the while loop is apparently going thru the results of a query. But the structure of the loop makes no sense to me.

 

Issue 1 - Where did $result_row come from? OK, I get it that PHP allows on the fly variable declaration. But $result_row has not even been assigned a value, so how can it be used to do a compare (as is being done below inside the condition part of the loop)?

 

Issue 2 - mysql_fetch_row(($result)) is supposed to output the first row of a query - right? So what makes it go to the next part of the query result when the loop runs the second time? Lets assume that $result = [A, B, C, D]. Then mysql_fetch_row($result) will always be A, no matter how many times the loop runs. But in reality it is iterating thru the result. How is that possible?

 

Issue 3 - Is there a more intuitive way to write the while loop so that it makes sense instead of the shortcut version?

While (end of array is not reached)

{

  Do stuff;

}

 

 

 

$query = "SELECT * FROM 'books' NATURAL JOIN 'authors'";

$result = mysql_query($query);
i=0;
while ($result_row = mysql_fetch_row(($result)))
{
   echo $result_row[i].'<br/>';
   i++;
}

 

TIA

 

Link to comment
Share on other sites

Issue 1 - Where did $result_row come from? OK, I get it that PHP allows on the fly variable declaration. But $result_row has not even been assigned a value, so how can it be used to do a compare (as is being done below inside the condition part of the loop)?

It is NOT being used as a compare it is doing an assignment. That if condition will return TRUE if $result_row can be assigned a value from the mysql_fetch_row() function. Each time that function is called the next record will be retrieved and - in this instance - assigned to $result_row. So, once the last record from the result set is used a new value is not assigned to $result_row and the condition is false.

 

Issue 2 - mysql_fetch_row(($result)) is supposed to output the first row of a query - right? So what makes it go to the next part of the query result when the loop runs the second time? Lets assume that $result = [A, B, C, D]. Then mysql_fetch_row($result) will always be A, no matter how many times the loop runs. But in reality it is iterating thru the result. How is that possible?

Same answer as above - as each record is extracted from the result set, the pointer moves to the next record in the result set. You should take a few minutes and read the manual.

 

Issue 3 - Is there a more intuitive way to write the while loop so that it makes sense instead of the shortcut version?

While (end of array is not reached)

{

  Do stuff;

}

 

Huh?! It makes prefect sense. Continue assigning the next record to $result_row until there are no more records left. Again, read the manual.

 

What doesn't make sense in that code is the use of $i. The way it is used it will show the first field from the first record, the second field from the second record, etc.

Link to comment
Share on other sites

1. It is not comparing ==  it is actually making the assignment = and then evaluating it to true or false.  After the last row it will return false and the loop will stop.

2. No, it advances to the next row.  That's how it works.  The manual says "Returns a numerical array that corresponds to the fetched row and moves the internal data pointer ahead. "

3. This is very common and I've never heard of it called a shortcut.

Link to comment
Share on other sites

$query = "SELECT * FROM 'books' NATURAL JOIN 'authors'";

$result = mysql_query($query);

while ($result_row = mysql_fetch_row(($result)))
{
   echo implode(', ', $result_row) . '<br/>';
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.