Echo mysqli result items

hi all

I am using a mysqli ‘select where’ query to return one row from a database that includes 4 items that I want to use in an html page (to display an image, title, description and alt text). The row is selected based on the unique title.

Currently I am using a fetch_assoc to get the data and then using a while loop to go through the results to build an array of the result. I then echo the html and array items to the page. This works fine.

However in this case, as I know there will only be one row returned and it would seem better to only echo the array items into the page html, rather than echo the array items and html together as part of the while loop.

However I tried to echo the array items outside of the while loop and they didn’t display.

Hopefully that makes sense?

so my question is:

  1. is fetch-assoc the best method to get this one row of data from the database?
  2. are the array items available outside the while loop and if so how do I reference them?

Many thanks in advance

Matt

It’s a long while since I used mysqli but I think for a single row you can use mysqli_fetch_array()

$row = mysqli_fetch_array($sql);

No need for any loops, just find the data in the array like: $row['title']

Or just use fetch_assoc() once without the loop, and echo the contents just like you would have done.

The contents being available is down to the fetch() function (whether you use fetch_assoc() or fetch_array() ), not the loop itself. My guess is that if you left the loop in place but moved the echo to after the loop, the problem you have is that by the time the loop has exited, the results will be blank:

while ($row = mysqli_fetch_assoc()) { 
  // $row has some information in it
  // so you can echo it here
  }
// $row is now equal to "false", which is why the loop was exited
// so there's nothing in it to echo

Thank you both.

That certainly sounds like the issue - as the content was showing in the loop but not outside of it.

Appreciate the advice, will try those methods tonight and report back.

Thanks a lot. Matt

Rather than echo your data into your page within the for loop you could store your row(s) into a variable. It would be a good idea to run your DB query at the beginning of your file, store it, and use the row from a variable throughout your file. Though this makes the most sense if you’re expecting larger amounts of data, not a single row of only four columns.

My suggestion would lead you to running more calls (while loop to store, call on variable data) where directly echoing it from the loop would be quicker.

Personally I prefer the fetch_array() as I always know to expect an array and the order of elements in that array can be defined within the SELECT query.

hi all

solved it with the advice above. Thanks!

although it as led me to one final question at the final piece of the puzzle which I will ask in another thread. Thanks for all the quick advice.

Best,
Matt

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.