holly9 Posted March 16, 2013 Share Posted March 16, 2013 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. Link to comment https://forums.phpfreaks.com/topic/275746-will-the-server-crash-if-i-do-over-50-mysql-querys-with-the-for-statement-to-retrieve-data/ Share on other sites More sharing options...
trq Posted March 16, 2013 Share Posted March 16, 2013 Probably not, but why would you use such terrible code? There are more efficient ways of doing this. Link to comment https://forums.phpfreaks.com/topic/275746-will-the-server-crash-if-i-do-over-50-mysql-querys-with-the-for-statement-to-retrieve-data/#findComment-1419040 Share on other sites More sharing options...
holly9 Posted March 16, 2013 Author Share Posted March 16, 2013 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... Link to comment https://forums.phpfreaks.com/topic/275746-will-the-server-crash-if-i-do-over-50-mysql-querys-with-the-for-statement-to-retrieve-data/#findComment-1419044 Share on other sites More sharing options...
trq Posted March 16, 2013 Share Posted March 16, 2013 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 methodYou can loop through all results returned.I have study from site like w3schools which didn't helpw3schools is terrible. Get yourself a decent book. Link to comment https://forums.phpfreaks.com/topic/275746-will-the-server-crash-if-i-do-over-50-mysql-querys-with-the-for-statement-to-retrieve-data/#findComment-1419046 Share on other sites More sharing options...
holly9 Posted March 17, 2013 Author Share Posted March 17, 2013 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 ?> Link to comment https://forums.phpfreaks.com/topic/275746-will-the-server-crash-if-i-do-over-50-mysql-querys-with-the-for-statement-to-retrieve-data/#findComment-1419068 Share on other sites More sharing options...
trq Posted March 17, 2013 Share Posted March 17, 2013 Why don't you post your problematic code then? Link to comment https://forums.phpfreaks.com/topic/275746-will-the-server-crash-if-i-do-over-50-mysql-querys-with-the-for-statement-to-retrieve-data/#findComment-1419073 Share on other sites More sharing options...
holly9 Posted March 17, 2013 Author Share Posted March 17, 2013 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); Link to comment https://forums.phpfreaks.com/topic/275746-will-the-server-crash-if-i-do-over-50-mysql-querys-with-the-for-statement-to-retrieve-data/#findComment-1419147 Share on other sites More sharing options...
holly9 Posted March 17, 2013 Author Share Posted March 17, 2013 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. Link to comment https://forums.phpfreaks.com/topic/275746-will-the-server-crash-if-i-do-over-50-mysql-querys-with-the-for-statement-to-retrieve-data/#findComment-1419178 Share on other sites More sharing options...
kicken Posted March 17, 2013 Share Posted March 17, 2013 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. Link to comment https://forums.phpfreaks.com/topic/275746-will-the-server-crash-if-i-do-over-50-mysql-querys-with-the-for-statement-to-retrieve-data/#findComment-1419180 Share on other sites More sharing options...
holly9 Posted March 17, 2013 Author Share Posted March 17, 2013 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!! Link to comment https://forums.phpfreaks.com/topic/275746-will-the-server-crash-if-i-do-over-50-mysql-querys-with-the-for-statement-to-retrieve-data/#findComment-1419187 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.