Jump to content

Will the server crash if i do over 50 mysql querys with the for statement to retrieve data?


holly9

Recommended Posts

Probably not, but why would you use such terrible code?

 

There are more efficient ways of doing this.

 

Hi, how is it bad please and what are the better ways to do this?.I recently learn php and only few weeks ago i got paid hosting to test codes.I didn't write out everything in the code and this is only an example.Mysqli fetch array only retrieve one data at a time which is the reason why i wanted to use this method.I also want to use it to display ads in a directory.I have study from site like w3schools which didn't help me to do the harder stuff...

Look at the logic of your code. The first iteration will produce:

SELECT * FROM Users WHERE UserID <= 1
The second:
SELECT * FROM Users WHERE UserID <= 2
All the way up to 60. Why not just use:
SELECT * FROM Users WHERE UserID <= 60
In the first place? Executing multiple queries when you don't have to is inefficient.

Mysqli fetch array only retrieve one data at a time which is the reason why i wanted to use this method

You can loop through all results returned.

I have study from site like w3schools which didn't help

w3schools is terrible. Get yourself a decent book.

 

Look at the logic of your code. The first iteration will produce:

SELECT * FROM Users WHERE UserID <= 1
The second:
SELECT * FROM Users WHERE UserID <= 2
All the way up to 60. Why not just use:
SELECT * FROM Users WHERE UserID <= 60
In the first place? Executing multiple queries when you don't have to is inefficient.You can loop through all results returned.w3schools is terrible. Get yourself a decent book.

Thanks. I did try looping through result before but it only gave me one row and each coloumn in one row is shown two times for example :name name,email email,and etc.This is the reason why i used the for statement before.i got warning about tags which confuse me because there wasn't much explanation.i'm assuming that mod is refering to these tags: <?php  ?>

it seems that the tos below has extra stuff than the tos given during signup so it took awhile to find the code tags info.i hope this is correct.

 

 
$result = $con->query("SELECT * FROM purchases ORDER BY ID LIMIT 5" );
$row = $result->fetch_array();


foreach ($row as $value)
  {
echo $value . "<br>";
  }




unset($value);
 

To get multiple rows out of a result set, you call the fetch function multiple times. This is commonly done using a while loop:

while ($row = $result->fetch_assoc()){
//process this row
}
Each time you call fetch_assoc the next row in the result set is returned. When there are no more rows left, false is returned which will cause the loop to exit.

 

To get multiple rows out of a result set, you call the fetch function multiple times. This is commonly done using a while loop:

while ($row = $result->fetch_assoc()){
  //process this row
}
Each time you call fetch_assoc the next row in the result set is returned. When there are no more rows left, false is returned which will cause the loop to exit.

 

Cool thanks allot it works!!.To test I use:

$result = $con->query("SELECT * FROM purchases ORDER BY ID LIMIT 5");


while ($row = $result->fetch_assoc()){
 foreach ($row as $value)
  {
echo $value . "<br>";
  }

unset($value);
}

 

I also test the code below to only select certain coloumn for all rows and it also works:

$result = $con->query("SELECT * FROM purchases ORDER BY ID LIMIT 5");


while ($row = $result->fetch_assoc())
{
 
echo $purchaserow['item'] . "<br>";
}
 

 

You both help me allot thanks!!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.