Jump to content

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


Go to solution Solved by kicken,

Recommended Posts

Hi I want to do this for many things on my php sites. Example:

 

 

for ($a=1; $a<=60; $i++)
{
$result = $con->query("SELECT * FROM Users WHERE UserID <='". $a ."'" );
$row = $result->fetch_array();
 
and etc
}
 

 

Thanks.

Edited by ignace
Added code tags

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);
 

I don't get double of everything any more when i use this

 $result->fetch_assoc();

instead of

 $result->fetch_array();

so the only problem i have is that i can't get many rows at a time with the looping.

  • Solution

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. Edited by kicken

 

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!!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.