ChenXiu Posted September 4, 2021 Share Posted September 4, 2021 This produces 10 results: $query = " select item from table order by date asc limit 10 "; while($row = $db->query($query)->fetch_assoc()) { echo $row["item"]; } Can PHP extract the first of the 10 results without running a duplicate query with "limit 1" in it? (I already tried things like echo $row[0] and echo $row[1] and print_r($row)). Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/313665-first-mysql-result-out-of-10-results/ Share on other sites More sharing options...
Barand Posted September 4, 2021 Share Posted September 4, 2021 You are calling a new query each time through the loop. Call the query then loop throught the results. Stop trying to do everything in a single line. Quote Link to comment https://forums.phpfreaks.com/topic/313665-first-mysql-result-out-of-10-results/#findComment-1589655 Share on other sites More sharing options...
ChenXiu Posted September 4, 2021 Author Share Posted September 4, 2021 Ha! You're onto me! No one liners. 😃 Something like this? $listings = ''; $query = " select item from table order by date asc limit 10 "; while($row = $db->query($query)->fetch_assoc()) { if(!isset($firstrow)) { $firstrow = $row["item"]; $listings = "The first row is $firstrow<br><br>"; } $listings .= $row["item"].'<br>'; } echo $listings; Quote Link to comment https://forums.phpfreaks.com/topic/313665-first-mysql-result-out-of-10-results/#findComment-1589656 Share on other sites More sharing options...
kicken Posted September 5, 2021 Share Posted September 5, 2021 (edited) 59 minutes ago, ChenXiu said: Something like this? No, you're still executing your query as part of the loop condition: 59 minutes ago, ChenXiu said: while($row = $db->query($query)->fetch_assoc()) { which means you run it again on every loop iteration. You need to run the query once before the loop, then call fetch_assoc() on each iteration of your result loop. Edited September 5, 2021 by kicken Quote Link to comment https://forums.phpfreaks.com/topic/313665-first-mysql-result-out-of-10-results/#findComment-1589658 Share on other sites More sharing options...
ChenXiu Posted September 5, 2021 Author Share Posted September 5, 2021 1 hour ago, kicken said: need to run the query once before the loop Thank you! Yes you are correct. I botched that one. And this is super basic stuff I should already know. Sometimes I wish I was in an actual (physical, not online) PHP classroom, and was made to learn PHP from the ground up, starting all the way from the beginning. There are so many rock-bottom absolute-basic things I have no clue about. One day, hopefully 😃 Quote Link to comment https://forums.phpfreaks.com/topic/313665-first-mysql-result-out-of-10-results/#findComment-1589660 Share on other sites More sharing options...
ChenXiu Posted September 5, 2021 Author Share Posted September 5, 2021 p.s. in my real scripts, I have:$result = $db->query($query); while ($row = $result->fetch_assoc()) { ... } (otherwise none of my mySQL queries would work) But when I type my question here, I goofed it up. I'm noticing the following: I have at least 20 new coding ideas every single day. Whenever I try them out, they *always* generate some sort of an error. ... and then I have to go fix the error. Each time. Looking back this entire year, I think every single block of code I've written has generated some sort of an error which I have to go fix. ... hopefully this gets better 😃 I feel like a first grader in english class.... "The dog jumped over the fense." - oops.... "fence" "The three bares liked porridge" - oops... "bears" while($row = $result = fetch_assoc().... Quote Link to comment https://forums.phpfreaks.com/topic/313665-first-mysql-result-out-of-10-results/#findComment-1589667 Share on other sites More sharing options...
Strider64 Posted September 5, 2021 Share Posted September 5, 2021 (edited) 2 hours ago, ChenXiu said: p.s. in my real scripts, I have:$result = $db->query($query); while ($row = $result->fetch_assoc()) { ... } (otherwise none of my mySQL queries would work) But when I type my question here, I goofed it up. I'm noticing the following: I have at least 20 new coding ideas every single day. Whenever I try them out, they *always* generate some sort of an error. ... and then I have to go fix the error. Each time. Looking back this entire year, I think every single block of code I've written has generated some sort of an error which I have to go fix. ... hopefully this gets better 😃 I feel like a first grader in english class.... "The dog jumped over the fense." - oops.... "fence" "The three bares liked porridge" - oops... "bears" while($row = $result = fetch_assoc().... A good thing to have is a good IDE that will point out the common syntax errors and find good resources/tutorials online to help with the logic flow. As for Database Tables I like using PDO and I still go to the following online resource when I get stump or have a brain fart. https://phpdelusions.net/pdo 🤣 I go here and other forums to see if someone before me has had the same problem and if not I will attempt to ask a question while showing my work. I learn over the years not to show too little or way too much code. I find commenting code help not only me but the people helping me trying to solve my problem(s). Edited September 5, 2021 by Strider64 1 Quote Link to comment https://forums.phpfreaks.com/topic/313665-first-mysql-result-out-of-10-results/#findComment-1589668 Share on other sites More sharing options...
ChenXiu Posted September 6, 2021 Author Share Posted September 6, 2021 9 hours ago, Strider64 said: A good thing to have is a good IDE That sounds like a great idea, thank you! I'll search for one now. Quote Link to comment https://forums.phpfreaks.com/topic/313665-first-mysql-result-out-of-10-results/#findComment-1589677 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.