Which is faster, if either...

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

turbo2ltr :

no offense, but i wouldn't use either one of those that you just posted heh.. neither one makes programical sense to me

this is much better :

Code: Select all

$field1 = array();
$field2 = array();
$field3 = array();
$result = mysql_query("SELECT * FROM rating_fields ");
while($row=mysql_fetch_assoc($result))
{
   $field1[] = $row['field1'];
   $field2[] = $row['field2'];
   $field3[] = $row['field3'];
}
for($i=0; $i<count($field1); $i++)
{
   //do something with one, or all of your $field variables
   /*  Example 

    echo $field1[$i];
    echo '<br />';
    echo $field2[$i];
    echo '<br />';
    echo $field3[$i];
    echo '<br /><br />';

   */
}
just my 2 cents worth though
turbo2ltr
Forum Commoner
Posts: 29
Joined: Sun Jul 18, 2004 4:08 pm

Post by turbo2ltr »

Your code doesnt do the same thing as mine. The point is I'm taking the result array (which is only one dimension) and iterating through it hundreds of times.

So is it faster just to access a local array with the data in it or to keep calling mysql_fetch_assoc to get the data out of the result array.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

to have each row as its own array (one-dimensional):

Code: Select all

$result = mysql_query("SELECT * FROM `rating_fields`");

$x = 0;
while(${"rows".(++$x)} = mysql_fetch_assoc($result)); // or whatever you prefer for your fetch..
to have each row as its own array (two-dimensional):

Code: Select all

$result = mysql_query("SELECT * FROM `rating_fields`");

while($rows[] = mysql_fetch_assoc($result)); // or whatever you prefer for your fetch..
If you want each field as it's own array (one-dimensional):

Code: Select all

$result = mysql_query("SELECT * FROM `rating_fields`");

while($row = mysql_fetch_assoc($result)) // or whatever you prefer for your fetch..
  foreach($row as $k => $v)
    ${"$k"}[] = $v;
If you want each field as it's own array (two-dimensional)

Code: Select all

$result = mysql_query("SELECT * FROM `rating_fields`");

while($row = mysql_fetch_assoc($result)) // or whatever you prefer for your fetch..
  foreach($row as $k => $v)
    $data[$k][] = $v;
It's not advisable to use fetch_array in any of those... #3 won't work with fetch_row...
turbo2ltr
Forum Commoner
Posts: 29
Joined: Sun Jul 18, 2004 4:08 pm

Post by turbo2ltr »

I'll assume that no one knows which is faster since none of the code snipets that were posted do anything close to what my code snipets do. :(
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your first one would be faster, as it's storing the data, instead of forcing sql to keep the data around..

#3 of mine is closest to your idea, except it creates an array (named the field name) for each of the fields returned.

data_seek is not recommended for large sets of iteration.
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

Post by Draco_03 »

for ($i=0,$i=sizeof($row);$i<$i;$i++)
You could do that

Code: Select all

for ($i=0, $size=sizeof($row); $i<$size; $i++)
that would work..
errr I think :P

EDIT : i forgot to put a coma :)
Post Reply