Close

August 10, 2011

How to Create an HTML Select Box From a MySQL Results Array Using PHP

This is a question I get quite a bit, so I figured I’d write a post on it:

How do I create an HTML select box with options from data in my database?

Here’s how:

The Query

First, let’s look at our query. Yours might look something like this:

That will select everything in our table and loop it into an array of objects containing the results. Now, we can take that array and create a HTML select box with it.

The HTML Select Box

Now, that we have our array of objects from our database we can use it to populate an HTML select box dynamically with PHP. Here’s how that might look:

That’s it. The code above will loop through the array creating a new option for each element in the array… and populating each option with the desired values from your database. Of course, this is just sample code, you’ll need to work with your database structure and code to make it work for you… but, that should give you the framework for getting this kind of thing done.

Here’s a video walk-through of this, as well:

If you enjoyed this tutorial and want to keep learning, check out my free tutorial site here: https://johnsfreetuts.com

5 Comments on “How to Create an HTML Select Box From a MySQL Results Array Using PHP

Giveaways
May 26, 2013 at 6:15 am

Very informative blog are over here! I wanna earn money through using PHP, CSS, and XHTML template for a website and hopefully this blog will help me a lot to prepare website by using this program language as well. So thanks for this informative blog.

Reply
Sajid Salim Khan
July 29, 2013 at 7:26 am

Dear John
The Post is very usefull but i have a Question why do we used Array_pop here as it shortens the result and when i removed the array_pop a long sentence was added to end of my select box "Trying to get property of non object"
Would u plz explain this

Regards Sajid salim Khan

Reply
John Morris
July 29, 2013 at 6:32 pm

If you watch the YouTube video on this page, I explain that in detail but basically it's because the while loop preceding it creates an extra blank element. So, we pop that blank element off the end in order to give us only the actual results.

You get the error in your select box, because whatever you're using to output to html is expecting an object… but the result is empty, so there's no object to output from.

Reply
Bidush Sarkar
January 17, 2019 at 8:01 am

how can i create a multi dropdown search panel and fetch result on the same page after selection of dropdown which will be submitted by the user and there should be an active and inactive button on the user panel if user activate then the data will be available in the result or if inactive then data will be be not visible see this reference website http://02hero.in if there any source code please provide it to me because I am new to php

thanks,
Bidush Sarkar

Reply
anayamartella
July 9, 2014 at 6:05 pm

Do you have a download for all those codes already? My own are all messed up and it would be awesome to see codes that work and how they work and follow along with your video properly. 🙂

Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.