Page 2 of 2
Posted: Tue Jul 20, 2004 10:21 pm
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
Posted: Tue Jul 20, 2004 10:33 pm
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.
Posted: Tue Jul 20, 2004 10:54 pm
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...
Posted: Wed Jul 21, 2004 7:11 pm
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.

Posted: Wed Jul 21, 2004 7:18 pm
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.
Posted: Thu Jul 22, 2004 8:05 am
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
EDIT : i forgot to put a coma
