0

I have created a series of web pages one creates users into a sql database another page looks up all of these users then needs to show them in a drop down list for another database input. On calling the php file and using echo to print out the array it works fine however when i put it within my html file the drop down menu simply displays "$username_array[] = "\"".$row['Username'].""\" once I believe its something to do with escaping quotes however cant figure it out any help would be greatly appreciated!!

This is the code held within the html file

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$db = 'fid';

echo "<label class=\"input\" for=\"investigator\" type=\"input\">Investigator:<select id=\"investigator\" name=\"investigator\">"; 

$conn = mysql_connect($dbhost,$dbuser,$dbpass);
if (!$conn)
die('Could not connect: ' . mysql_error());

mysql_select_db($db);

$username_array = array();
$sql = mysql_query("SELECT `Username` FROM `user`");
while ($row = mysql_fetch_array($sql)){
//echo $username_array[] = "\"".$row['Username']."\"";
echo "<option value='null'>"$username_array[] = "\"".$row['Username'].""\"</option>";
}
echo "</label>";

mysql_close($conn)
?>

I want the username array to display one user after the other within a drop down list however currently just echos out the option value line without any interpretation

UPDATE

I've now changed the code to this

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$db = 'fid';

$conn = mysql_connect($dbhost,$dbuser,$dbpass);
if (!$conn)
die('Could not connect: ' . mysql_error());

mysql_select_db($db);

echo '<select id="investigator" name="investigator">';
$resource = mysql_query("SELECT `Username` FROM `user`");
if($resource && mysql_num_rows($resource)) {
    while ($row = mysql_fetch_assoc($resource)){
        echo '<option value="'.$row['Username'].'">'.$row['Username'].'</option>';
    }
}
echo '</select>';
mysql_close($conn)
?>

however the html outputs the script rather than a drop down list and the usernames

html output

'; $resource = mysql_query("SELECT Username FROM user"); if($resource && mysql_num_rows($resource)) { while ($row = mysql_fetch_assoc($resource)){ echo ''; } } echo ''; mysql_close($conn) ?>

any help would be appeciated I just really need to get this working as its been bugging me for days!

1
  • Is that code a direct copy paste? or are some copy paste problems to be expected? Commented Mar 19, 2013 at 14:34

2 Answers 2

1

This will do what you need

echo '<select id="investigator" name="investigator">';
$resource = mysql_query("SELECT `Username` FROM `user`");
if($resource && mysql_num_rows($resource)) {
    while ($row = mysql_fetch_assoc($resource)){
        echo '<option value="'.$row['Username'].'">'.$row['Username'].'</option>';
    }
}
echo '</select>';

BUT you should also move to mysqli or PDO as mysql_* is depreciated

I have changed $sql to $resource as it is not an SQL statement but a resource.

Sign up to request clarification or add additional context in comments.

2 Comments

I omitted the <SELECT> as OP has the start in prior code. But Daedalus shows correct example (properly closed)
I was writing the same answer! You allow me to win time... ^^
0

I'd change your code to something similar to this:

echo "<select>";
while ($row = mysql_fetch_array($sql)) {
    echo "<option value=\"VALUE\">".$row['Username']."</option>";
}
echo "</select>";

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.