FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » mysql_fetch_array
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: mysql_fetch_array [message #169809 is a reply to message #169808] Mon, 27 September 2010 13:35 Go to previous messageGo to previous message
Captain Paralytic is currently offline  Captain Paralytic
Messages: 204
Registered: September 2010
Karma:
Senior Member
On 27 Sep, 14:18, MikeB <mpbr...@gmail.com> wrote:
> matt wrote:
>> On Sep 25, 4:28 pm, MikeB<mpbr...@gmail.com>  wrote:
>>> I have the following code:
>
>>> $query = "SELECT * FROM classics";
>>> $result = mysql_query($query);
>
>>> if (!$result) die ("Database access failed: " . mysql_error());
>>> $rows = mysql_num_rows($result);
>
>>> for ($j = 0 ; $j<  $rows ; ++$j)
>>> {
>>>          $results[] = mysql_fetch_array($result);
>
>>> }
>
>> This doesn't make much sense to me, unless you need the value of $rows
>> later on...
>
>> how about this instead:
>
>> if (!$result = mysql_query("..."))
>>    die ("...");
>
>> while ($row = mysql_fetch_array($result))
>>    $results[] = $row;
>
>> You're defining two additional variables that are totally unneeded.
>> Of course some pedant is probably going to jump down my throat for
>> putting assignments in my conditionals...
>
> How efficient is it to call the mysql_fetch_array($result) function
> every time for the the loop  evaluation vs.comparing  with a variable
> that is set once?

You have me completely confused now!!!

Surely:
> for ($j = 0 ; $j < $rows ; ++$j)
> {
> $results[] = mysql_fetch_array($result);
> }
also calls mysql_fetch_array($result) just as many times as the other
one (OK one less, but you make up for that by calling
mysql_num_rows($result)).

Since in order to use the contents of $results[] you have to execute
another loop, I'm sure you can work out which is the more efficient.
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Group sort syntax?
Next Topic: Code to create a cookie in PHP
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Wed Nov 27 13:05:14 GMT 2024

Total time taken to generate the page: 0.04052 seconds