Jump to content

first mySQL result out of 10 results


ChenXiu

Recommended Posts

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.

 

Link to comment
Share on other sites

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;

 

Link to comment
Share on other sites

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 by kicken
Link to comment
Share on other sites

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 😃

Link to comment
Share on other sites

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()....
 

Link to comment
Share on other sites

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 by Strider64
  • Like 1
Link to comment
Share on other sites

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.