Loops

taekwondokid42

New Member
Messages
268
Reaction score
0
Points
0
$result1 = mysql_query("SELECT * FROM people WHERE id = '$number[0]'") or die(mysql_error());
$row1 = mysql_fetch_array( $result1 );
$result2 = mysql_query("SELECT * FROM people WHERE id = '$number[1]'") or die(mysql_error());
$row2 = mysql_fetch_array( $result2 );
$result3 = mysql_query("SELECT * FROM people WHERE id = '$number[2]'") or die(mysql_error());
$row3 = mysql_fetch_array( $result3 );
$result4 = mysql_query("SELECT * FROM people WHERE id = '$number[3]'") or die(mysql_error());
$row4= mysql_fetch_array( $result4 );
$result5 = mysql_query("SELECT * FROM people WHERE id = '$number[4]'") or die(mysql_error());
$row5 = mysql_fetch_array( $result5 );

echo $row1[answer];
echo $row2[answer];
echo $row3[answer];
echo $row4[answer];
echo $row5[answer];



Is there anyway I can put this code into a loop?
 
Last edited:

Slothie

New Member
Messages
1,429
Reaction score
0
Points
0
PHP:
$result = mysql_query("SELECT * FROM people WHERE id in  ($number[1],$number[2],$number[3],$number[4],$number[5]) ") or die(mysql_error()); 

while (row5 = mysql_fetch_array( $array)) {
echo $row[$answer];

}
 

rickle42

New Member
Messages
59
Reaction score
0
Points
0
$temp=0;
while(echo mysql_query("SELECT answer FROM people WHERE
id = '$number[$temp++]'")){echo '\n';}


does this work?
 
Last edited:

Slothie

New Member
Messages
1,429
Reaction score
0
Points
0
You can't echo a mysql_query directly and by the logic, it'd work if the numbers are sequential.
 

Thewinator

New Member
Messages
256
Reaction score
0
Points
0
PHP:
...
while (row5 = mysql_fetch_array( $array)) {
...


Isn't that supposed to be

PHP:
...
while ($row = mysql_fetch_array( $array)) {
...

And another loop could be
PHP:
for($iIndex=0; $iIndex<5; $iIndex++)
{
$result{$iIndex} = mysql_query("SELECT * FROM people WHERE id = '$number[$iIndex]'") or die(mysql_error()); 
$row{$iIndex} = mysql_fetch_array( $result{$iIndex} );
echo $row{$iIndex}[answer];
}
 
Last edited:

Livewire

Abuse Compliance Officer
Staff member
Messages
18,169
Reaction score
216
Points
63
2 questions based on the recent loops that've gone through actually:

1) Is it faster to do everything in one query then sort it out via php code (read: in this case, grab everyone and sort by id, then run a loop via php to check all the people and see if the id's match)?

2) Is it less intensive on server resources to grab it all, then sort via php code?


Asking cause if either one is true, then theres no reason to run 5 separate queries. It's not necesarilly gunna kill a small site, but if you have 100 users an hour (just an example), you can either do 100 queries with some php code to sort the results how you want, or you can do 500 to do 5 queries a user.

I mean, again, not a big difference. But say a query takes 0.1 seconds (thats 1/10th if you can't see the decimal); that's 10 seconds if you do 1 query a user, or 50 if you do 5. That'd be enough to slow things down a bit, especially since I doubt those'd be the ONLY 5 queries done (one particular page on my site is running 7, but I'm working out how to condense that).


Just curious. More cause if you do anything where it's gunna be heavilly used, it's obviously better to make it take the least time possible (so server resource usage script doesn't hit you, cause it isn't lasting long enough to make the script mad), and to use as few resources as possible (again, so the SRU script doesn't hit you until you get way too many members).


My guess? My guess is it's less intensive to grab it once, but it might not show up with a small DB.

My reasoning for this: with each query, wouldn't MySQL have to go through the entire database AGAIN? I'd rather just send it through the DB once with a single query and get all the main results, then go through it with php and check the 5 values ($number[0], $number[1], etc) rather than have to send MySQL through the DB a second, third, fourth, and fifth time to get more data.


-=Livewire's Crappy Analogy Time=-
You don't go to the store to buy the cake mix, come home, put it away, then go back and get the eggs, come home, and put it away, then go back and get the milk, come home, put it away, then go back to get the candles, come home, put it away, then go get the matches, come home, and put them away.

You go to the store and buy the cake mix, eggs, milk, candles, and the matches, then you sort it out when you get home.



Again, dunno if I'm even right. I'd just think it'd be easier to grab it in one shot then let php sort it out, rather than make MySQL sift a possibly-10,000 entry database 5 times. I HIGHLY doubt it'd make a difference in a small DB (having mysql go through 5 lines probably takes about as long as going through 6, so it doesn't matter that much on something small), but a large one for a popular forum?
 

Slothie

New Member
Messages
1,429
Reaction score
0
Points
0
Its less intensive to do it in MySQL, after all that's what databases are for, for you to manipulate data as you see fit.

@thewin
PHP:
for($iIndex=0; $iIndex<5; $iIndex++)
{
$result{$iIndex} = mysql_query("SELECT * FROM people WHERE id = '$number[$iIndex]'") or die(mysql_error()); 
$row{$iIndex} = mysql_fetch_array( $result{$iIndex} );
echo $row{$iIndex}[answer];
}

That'd work assuming its sequential data he wanted. It could be shortened to
PHP:
for($iIndex=0; $iIndex<5; $iIndex++)
{
$result= mysql_query("SELECT * FROM people WHERE id = '$number[$iIndex]'") or die(mysql_error()); 
$row = mysql_fetch_array( $result );
echo $row[answer];
}
So you wouldn't have to create 5 different values in memory as you only need the current one. Not much difference for 5 records but for 500? 5000? Notice that yours also queries the database 1 time for each record, if he needed 500 records that'd be 500 queries.

Looks like a red flag from x10's resources watchdogs :p
 

Livewire

Abuse Compliance Officer
Staff member
Messages
18,169
Reaction score
216
Points
63
Making sure I read it properly; it's easier to sort it in the sql database, but at the same time it's a BAD idea to use 500 to build the page? :p

I'll keep that in mind. I still need to shorten my 7 query page down though, it shouldn't need 7 to display the news >_<
 

Slothie

New Member
Messages
1,429
Reaction score
0
Points
0
I meant, its a bad idea to use too many queries. Its perfectly okay to retrieve 500 records but sorting them in PHP may not be the best idea out there.
 
Top