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 7581131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:07:32+00:00 2026-05-30T18:07:32+00:00

I am a beginner to MySQL and Im trying to retrieve a name for

  • 0

I am a beginner to MySQL and Im trying to retrieve a name for a specific ID.

Here is how my database looks like:

I have a table of contacts which contains ID, first and last name.

I want to retrieve the first and last name for a specific ID in the same form.

So I want to end up with a form of options and the option value will be the first name for each ID.

For example:

<form>
<select name="users">
<option value="">Select a person:</option>
<option value="5"> <?php echo "$first $last" ?> </option>
<option value="10"> <?php echo "$first $last" ?> </option>
<option value="15"> <?php echo "$first $last" ?> </option>
</select>
</form>

The values are the ID’s.

and my PHP:

<?php   include("connection.php");

$query="SELECT * FROM contacts";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

$i=0;
while ($i < $num) {

$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");

$i++;
}

?>

in my actual example I get the first and last name of the last’s person in the database only.

So how can I retrieve the first and last name for specific ID’s of which I assign in my form (?)

  • 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-30T18:07:34+00:00Added an answer on May 30, 2026 at 6:07 pm

    This is because your while loop is overwriting $first and $last every single time. Try

    $users = array();
    
    while ($row = mysql_fetch_assoc($result)) { 
      $users[] = $row;
    }
    

    And in HTML (EDITED):

    <form>
      <select name="users">
        <option value="">Select a person:</option>
        <?php foreach($users as $row): ?>
          <option value="<?php echo $row['id']; ?>"> <?php echo $row['first'] . " " .  $row['last']; ?> </option>
        <?php endforeach; ?>
      </select>
    </form>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Absolute beginner question: I have a template file index.html that looks like this: ...
all.I'm a beginner of Mysql and I have one simple question here. I want
i am a beginner of MYSQL please help me. i have a two table
I have MySQL tables looking like this: regions table id | region ------------------- 1
I have stumped on this as I am a total beginner in MySql. Here
Here's my background: Decent experience with PHP/MySql. Beginner's experience with OOP Why I want
trying to migrate my existing asp.net website which is using mysql to Windows Azure
MySQL beginner here. I had thought that copying the directory <mysql_path>/<data_base_name> to USB stick
I am a beginner at using mysql and I am trying to learn the
Hey, a beginner's question here.... In order to learn PHP and SQl I have

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.