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

DISCLAIMER: This post may contain “affiliate” links to products and services I recommend. I’ll receive a small commission if you decide to purchase one of these products or services. I only recommend products I genuinely believe will help you in running your freelance business.

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

You might also like

Do you want more freelance clients?

I’ll show you what I learned over the last 15 years to grind out (from absolute scratch) a backlog of new clients wanting to hire you. Who your best client prospect are, what services you should be offering them, where to find them and more. Just enter your email address in the box below and let’s get started:

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on reddit
Reddit
Share on pinterest
Pinterest
John Morris

JOHN MORRIS

I’m a 15-year veteran of freelance web development. I’ve worked with bestselling authors and average Joe’s next door. These days, I focus on helping other freelancers build their freelance business and their lifestyles.

This Post Has 5 Comments

  1. 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.

  2. 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

    1. 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.

      1. 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

  3. 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. 🙂

Leave a Reply

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

Do you want more freelance clients?

Enter your email below to get started building your system for consistently bringing in new freelance clients:

WHAT OTHERS ARE SAYING

Lori Grant

John did an outstanding job on my project. I highly recommend him and look forward to working with him on future projects.

Daniel Mohlendick

On the Freelancing on Upwork course: “This is by far the best course i have watched on Skillshare!! Thank you so much.”

Lewis Howes

John is amazing at building membership sites. He converted one of my sites over from it’s existing (hardly working) platform over to the clean and simple to use WishList membership platform. I highly recommend using John and WishList for any of your membership site needs.

Bradley Smith

John and I have worked together on numerous projects. John is very quick and efficient and was a pleasure to work with.

Bob Patterson

Not only is John a very talented programmer and developer, he is also an excellent communicator. He has a talent for taking complex subjects and communicating them in terms that anyone can understand. This is a rare combination. This ability has enabled me to take my skills and knowledge to the next level. Thank you John for for all that you do.

Chris Aitken

He significantly improved my site through his expert knowledge of PHP, CSS and Javascript. Would definitely recommend John to others.

Xan Barksdale

Xan Barksdale

Very professional worker who is extremely knowledgable in WordPress and Wishlist Member. I would definitely hire again.

Close Menu