PHP While Loop drop down menu problem

Posted on 2007-10-02
Last Modified: 2013-12-13
I have this problem with a while loop that keeps returning the first initial of someone's name when it runs. I want the full name. The loop is designed to create a drop down list that works fine, except for this stupid inconsistency. I checked the database, and the full name is definitely there. The last name comes out fine, so I dont know where the problem is. Here is the code:

$resulttwo=mysql_query("SELECT * FROM employees WHERE empnum>=1 ORDER BY firstname ASC") or die(mysql_error());

$displayth ='<select name="th" id="th">';


while ($rowtwo=mysql_fetch_array($resulttwo))
$displayth .='<option value="' . $empid[$p] . '">' . $firstname[$p] . " " . $lastname[$p] . '</option>';
$displayth .= '</font><font size="2" face="Eras Demi ITC">';

$displayth .='</select>';

echo $displayth;
Question by:jcbodyworks
    LVL 13

    Accepted Solution

    I see nothing wrong with your code. I realize you've checked your database, but it would probably be a good idea to check your output as well. Instead of using $firstname[$p] for your option text, try using $rowtwo['firstname']. Find out what's actually being pulled from the database. Also, since you're calling the variables as associative arrays, it might be a good idea to change "mysql_fetch_array" to "mysql_fetch_assoc".

    This probably won't fix your error, but it might improve your performance slightly.

    Author Comment

    I just looked through the entire code of my site, turns out I have another variable like this:


    this was causing the problem. I changed the variable, so now everything works like a charm! Thank you!
    LVL 13

    Expert Comment

    Hey, good job! Thanks for the points, and good luck.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    As this topic comes over and over again in different forms, I've finally decided to write a short (yea, right...) article / tutorial about pagination with PHP with MySQL database. There are dozens of these kind of tutorials, I know - I wanted to mak…
    This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing s…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to count occurrences of each item in an array.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now