Which is faster, if either...
Moderator: General Moderators
Which is faster, if either...
I need to iterate through the results of 1 query multiple (possibly hundreds) of times.
Which is faster:
Just keep calling mysql_fetch_array to get the rows, then calling mysql_data_seek every time I want to reset the array pointer and iterate again,
OR
Copy all the rows from mysql_fetch_array into a local array and just use the local array to do the iteration?
The data in the array/result doesn't change.
Thanks,
Mike
Which is faster:
Just keep calling mysql_fetch_array to get the rows, then calling mysql_data_seek every time I want to reset the array pointer and iterate again,
OR
Copy all the rows from mysql_fetch_array into a local array and just use the local array to do the iteration?
The data in the array/result doesn't change.
Thanks,
Mike
- evilmonkey
- Forum Regular
- Posts: 823
- Joined: Sun Oct 06, 2002 1:24 pm
- Location: Toronto, Canada
If you're asking what I think you're asking, I suggest this:
Cycle through the results like this:
Good luck!
Code: Select all
for($i=0;$i<mysql_num_rows($result);$i++){
$row[] = mysql_fetch_assoc($result);
//where result is you mysql_queryCode: Select all
for($i=0,$i=sizeof($row);$i<$i;$i++){
echo $row[i]['field'] //blah blah blahWhen will $i ever be less than itself?evilmonkey wrote:Code: Select all
($i=0,$i=sizeof($row);$i<$i;$i++)
well, that coudl also be done like :evilmonkey wrote:If you're asking what I think you're asking, I suggest this:
Cycle through the results like this:Code: Select all
for($i=0;$i<mysql_num_rows($result);$i++){ $row[] = mysql_fetch_assoc($result); //where result is you mysql_queryGood luck!Code: Select all
for($i=0,$i=sizeof($row);$i<$i;$i++){ echo $row[i]['field'] //blah blah blah
Code: Select all
while($row = mysql_fetch_assoc($result))
{
echo $row['myfield'];
// or just $field1[] = $row['field1']; $field2[] = $row['field2']; etc..
// of course declaring the variables you want to use as arrays before hand
}Code: Select all
for($i=0; $i<count($field1); $i++)
{
echo 'Do Something with '.$field1[$i].' or '.$field2[$i];
}- evilmonkey
- Forum Regular
- Posts: 823
- Joined: Sun Oct 06, 2002 1:24 pm
- Location: Toronto, Canada
My stupididty, that shouldn't be there.nigma wrote:When will $i ever be less than itself?evilmonkey wrote:Code: Select all
($i=0,$i=sizeof($row);$i<$i;$i++)
Why would you use mysql_fetch_assoc() when you can use mysql_fetch_array()?
Say you have the following declarations:
As far as I know you can, in both cases, get the value of the username field by doing the following:
Say you have the following declarations:
Code: Select all
$query1 = $query2 = mysql_query("select username from table");
$row1_query1 = mysql_fetch_array($query1);
$row2_query2 = mysql_fetch_array($query2);Code: Select all
echo $row1_query1['username'];
echo $row2_query2['username'];- evilmonkey
- Forum Regular
- Posts: 823
- Joined: Sun Oct 06, 2002 1:24 pm
- Location: Toronto, Canada
Ok, guess I didn't explain it well enough..
which is faster:
OR
edit: fixed code errors.....
which is faster:
Code: Select all
$result = mysql_query("SELECT * FROM rating_fields ");
$x=0;
while($field_s = mysql_fetch_array($result ,MYSQL_ASSOC))
{
$fieldarrayї$x] = $field_sїrating_tablename];
$x++;
}
for($y=0;$y< someval;$y++)
{
foreach($fieldarray as $field)
{
// do something with $field
}
}Code: Select all
$result = mysql_query("SELECT * FROM rating_fields ");
for($y=0;$y< someval;$y++)
{
while($field = mysql_fetch_array($result ,MYSQL_ASSOC))
{
// do something with $field
}
mysql_data_seek($result, 0);
}
Last edited by turbo2ltr on Tue Jul 20, 2004 10:12 pm, edited 3 times in total.
- evilmonkey
- Forum Regular
- Posts: 823
- Joined: Sun Oct 06, 2002 1:24 pm
- Location: Toronto, Canada
=PPHP Manual wrote: mysql_fetch_assoc() is equivalent to calling mysql_fetch_array() with MYSQL_ASSOC for the optional second parameter. It only returns an associative array. This is the way mysql_fetch_array() originally worked. If you need the numeric indices as well as the associative, use mysql_fetch_array().