$sqlStr = "selec * from customers";
$results = mysql_query($sqlStr);
// 1)
$recNum = mysql_num_rows($results);
for($i=0; $i<$recNum; $i++)
{
$name = mysql_result($results,$i,name);
$address = mysql_result($results,$i,address);
...
}
// 2)
while($row=mysql_fetch_array($results))
{
$name = $row[name];
$address = $row[address];
...
}
1) or 2)?I can only guess, but 2 uses fewer functions, so it might be faster. But I don't have any in-depth info about how PHP works and handles memory. You could always test it, by timing how long it takes to generate the same page, using both methods. You would have to have a lot of data in the DB to notice a difference, but I guess you do since you're concerned about the speed and time.
I use method 2. But do note that you must use quotes when accessing associative arrays. $row["name"].as far as i know, there is no difference in speed between for and while loops, so it would be the samei agree with n8 on this one, i dont think that there is a speed difference on loopsYeah, with..
for($i=0;$i<100;$i++)
echo "blah\n";
..and..
$i=0;
while($i<100){
echo "blah\n";
$i++;
}
..there is certainly microscopical, if any difference at all. But the ones in his example differ in more ways than that, dont they?well although you guys are right, the loops are the same, BUT you can't look at the loops.
mysql_result is always slower than mysql_fetch_array.So, obviously..
$address = mysql_result($results,$i,address);
is SLOWER than
$address = $row[address];
in above example.
And I guess the other factors like
mysql_num_rows and mysql_fetch_array
are not significant?
So in real life, 2) will be faster even with 1 more extra line in the while condition?
$results = mysql_query($sqlStr);
// 1)
$recNum = mysql_num_rows($results);
for($i=0; $i<$recNum; $i++)
{
$name = mysql_result($results,$i,name);
$address = mysql_result($results,$i,address);
...
}
// 2)
while($row=mysql_fetch_array($results))
{
$name = $row[name];
$address = $row[address];
...
}
1) or 2)?I can only guess, but 2 uses fewer functions, so it might be faster. But I don't have any in-depth info about how PHP works and handles memory. You could always test it, by timing how long it takes to generate the same page, using both methods. You would have to have a lot of data in the DB to notice a difference, but I guess you do since you're concerned about the speed and time.
I use method 2. But do note that you must use quotes when accessing associative arrays. $row["name"].as far as i know, there is no difference in speed between for and while loops, so it would be the samei agree with n8 on this one, i dont think that there is a speed difference on loopsYeah, with..
for($i=0;$i<100;$i++)
echo "blah\n";
..and..
$i=0;
while($i<100){
echo "blah\n";
$i++;
}
..there is certainly microscopical, if any difference at all. But the ones in his example differ in more ways than that, dont they?well although you guys are right, the loops are the same, BUT you can't look at the loops.
mysql_result is always slower than mysql_fetch_array.So, obviously..
$address = mysql_result($results,$i,address);
is SLOWER than
$address = $row[address];
in above example.
And I guess the other factors like
mysql_num_rows and mysql_fetch_array
are not significant?
So in real life, 2) will be faster even with 1 more extra line in the while condition?