jd2007 Posted August 16, 2007 Share Posted August 16, 2007 if i want to display each row from a table: $que1="select MAX(id) from table"; $res1=mysql_query($que1); $row1=mysql_fetch_row($res1); for ($i=0; $i<=$row1[0]; $i++) { $que2="select cola from table where id='$i'"; $res2=mysql_query($que2); $row2=mysql_fetch_row($res2); echo $row2[0]."<br />"; } what function to use or query to use to make the above simpler ? Link to comment https://forums.phpfreaks.com/topic/65191-is-there-a-simpler-way-to-this-below-mysql-query/ Share on other sites More sharing options...
gurroa Posted August 16, 2007 Share Posted August 16, 2007 $res = mysql_query("select cola from table"); while($row = mysql_fetch_assoc($res)) { echo $row['cola']."<br />"; } Link to comment https://forums.phpfreaks.com/topic/65191-is-there-a-simpler-way-to-this-below-mysql-query/#findComment-325506 Share on other sites More sharing options...
jd2007 Posted August 16, 2007 Author Share Posted August 16, 2007 thanks...is it wrong to use the way i used ? Link to comment https://forums.phpfreaks.com/topic/65191-is-there-a-simpler-way-to-this-below-mysql-query/#findComment-325508 Share on other sites More sharing options...
tibberous Posted August 16, 2007 Share Posted August 16, 2007 It's slower the way you did it - guess you could say it's wrong. Link to comment https://forums.phpfreaks.com/topic/65191-is-there-a-simpler-way-to-this-below-mysql-query/#findComment-325511 Share on other sites More sharing options...
NArc0t1c Posted August 16, 2007 Share Posted August 16, 2007 It's slower the way you did it - guess you could say it's wrong. Actually a clever way, most people would not have thought of it. It's another method of doing it, it's not "Wrong". Link to comment https://forums.phpfreaks.com/topic/65191-is-there-a-simpler-way-to-this-below-mysql-query/#findComment-325513 Share on other sites More sharing options...
Orio Posted August 16, 2007 Share Posted August 16, 2007 It's not wrong, but its much more better querying the database only once compared to who knows how many times- better performance. Orio. Link to comment https://forums.phpfreaks.com/topic/65191-is-there-a-simpler-way-to-this-below-mysql-query/#findComment-325519 Share on other sites More sharing options...
gurroa Posted August 16, 2007 Share Posted August 16, 2007 Actually a clever way, most people would not have thought of it. It's another method of doing it, it's not "Wrong". Disagree. Imagine that you have three rows in such table id cola 85 boo 93 foo 132 boo2 Now you will query dbase 133times to read 3 rows with 129 error messages. Link to comment https://forums.phpfreaks.com/topic/65191-is-there-a-simpler-way-to-this-below-mysql-query/#findComment-325523 Share on other sites More sharing options...
NArc0t1c Posted August 16, 2007 Share Posted August 16, 2007 133 times? And error messages could be avoidable(ever heard of the at(@) sign). I just cannot think of any examples on how I could use that for an application that would be better than a while loop(maybe there isn't). But it is "unique", and not wrong. Link to comment https://forums.phpfreaks.com/topic/65191-is-there-a-simpler-way-to-this-below-mysql-query/#findComment-325533 Share on other sites More sharing options...
jd2007 Posted August 16, 2007 Author Share Posted August 16, 2007 is it ok to use my way ? Link to comment https://forums.phpfreaks.com/topic/65191-is-there-a-simpler-way-to-this-below-mysql-query/#findComment-325539 Share on other sites More sharing options...
NArc0t1c Posted August 16, 2007 Share Posted August 16, 2007 is it ok to use my way ? For what you wanted to do, it's more efficiant to use a while loop. Link to comment https://forums.phpfreaks.com/topic/65191-is-there-a-simpler-way-to-this-below-mysql-query/#findComment-325542 Share on other sites More sharing options...
gurroa Posted August 16, 2007 Share Posted August 16, 2007 133 times? First is the query for "select MAX(id)". And the loop try to fetch 132 rows started from id = 1 to id = 132. Link to comment https://forums.phpfreaks.com/topic/65191-is-there-a-simpler-way-to-this-below-mysql-query/#findComment-325543 Share on other sites More sharing options...
mmarif4u Posted August 16, 2007 Share Posted August 16, 2007 Agree with gurroa . The best option is to use while loop. Link to comment https://forums.phpfreaks.com/topic/65191-is-there-a-simpler-way-to-this-below-mysql-query/#findComment-325547 Share on other sites More sharing options...
NArc0t1c Posted August 16, 2007 Share Posted August 16, 2007 133 times? First is the query for "select MAX(id)". And the loop try to fetch 132 rows started from id = 1 to id = 132. I'm not disagreeing that it will loop 133 times, I wanted to know why. It starts counting at 0 not one, that's why it has 133 loops. for ($i=0; $i<=$row1[0]; $i++) Agree with gurroa . The best option is to use while loop. For what jd2007 wants it for, yes. Link to comment https://forums.phpfreaks.com/topic/65191-is-there-a-simpler-way-to-this-below-mysql-query/#findComment-325552 Share on other sites More sharing options...
jd2007 Posted August 18, 2007 Author Share Posted August 18, 2007 thank you ! Link to comment https://forums.phpfreaks.com/topic/65191-is-there-a-simpler-way-to-this-below-mysql-query/#findComment-327384 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.