Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4000154
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:44:47+00:00 2026-05-20T07:44:47+00:00

I have a field in a MySQL database called ‘location’ – it serves up

  • 0

I have a field in a MySQL database called ‘location’ – it serves up North, South, East, West only.

I have used this code to get only the 4 results that are distinct:

$query_form = "SELECT DISTINCT location FROM hotel ORDER BY location ASC";
$result_form = mysqli_query($dbc, $query_form) or die('die query error');
$row_form = mysql_fetch_array($result_form, MYSQLI_NUM);

I wanted to use the four results from this query to populate a table such that:

<option value='1'>North</option>
<option value='2'>South</option>
<option value='3'>East</option>
<option value='4'>West</option>

I have used this:

foreach ($row_form['location'] as $k => $v) {
    echo '<option value="' . $k . '">' . $v . '</option>\n';
    }

but I fear that my approach will not work – with regret, I am a noob and unable to work out what is wrong!

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:44:48+00:00Added an answer on May 20, 2026 at 7:44 am

    mysql_fetch_array will only (quoting the manual, emphasis mine) :

    Fetch a result row as an
    associative array, a numeric array, or
    both

    So, you’ll have to call this function several times — actually, once per row (so, here, 4 times).

    This is normally done with a loop, looping while you get results.

    In your case, you’ll probably want to use something that looks like this :

    $lineCounter = 1;
    while ($row = mysql_fetch_array($result_form, MYSQL_ASSOC)) {
        // Use your row, here.
        // For instance : $row['location']
        // And you can use the $lineCounter variable, to keep track of the current line number.
        $lineCounter++;
    }
    

    Notes :

    • I used MYSQL_ASSOC, to get an associative array for each row : I think it’s easier to work this way.
    • In the body of the while loop, you can use the $row associative array, indexed by column name.

    As a sidenote, when injecting data (such as strings) in some HTML code, you should escape the output properly, to avoid HTML injections.

    See, for a solution, the htmlspecialchars function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL database table C with a field called Phone . I've
I have an items table in my MySQL database with a DECIMAL(10,2) field called
i have a mySql database, and it has an field for each entry called
I am working in PHP/MySQL. I have a table in my database called hourly
Ok so i have a database table called requests with this structure mysql> desc
I have a mysql database table called POST . One of the fields is
I have a mysql database which as one of the fields contains a html
I have a field in a database that is nearly unique: 98% of the
I have a field (say, foo) in a table in a SQL Server database
While porting data from one mysql database to another there have come some issues

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.