0

i'm relatively new to this trying to get a mysql database array of Username to be shown in a html drop down input for another form however the php script just keep being shown rather than the function. below is a screenshot of the error

http://s10.postimage.org/j4xuamkwp/untitled.png

the php script is sat within my html file

<?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 '<label class="input" for="investigator" type="input">Importance:</label><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)
?>

I think its an issue within the while loop however cannot fix it and its getting very frustrating!

2

3 Answers 3

2

Your file needs to have a .php extension to work properly.

.html will not be recognized as a php file.

Name your file: myfile.php for files that have php code in them. NOT myfile.html

EDIT: As others have pointed out, you can add html as a php type, but that's not typically how it's done, nor would I recommend it.

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

3 Comments

Hm.. true but not entirely. You can run .html as .php by using AddType application/x-httpd-php .html .htm
nah not true can tell server to use .bla file like php
Thank you i new it would be something small affecting it problem solved!
0

It sounds like your server isn't parsing PHP properly. Couple troubleshooting tips-

1) make sure you're PHP is enabled on the server.
2) make sure the extension of your file is ".php".
3) If you would like your server to parse ".htm|html" files as PHP, you can change your .htaccess (assuming you are using apache), by following these steps.

Comments

0

Yep, .html will not be read by the PHP interpreter (the part of the web server that reads and computes PHP scripts).

If you rename your file with a .php ending, for example file.php (not file.html) your script should work.

This part of your script has me confused:

if (!$conn)
die('Could not connect: ' . mysql_error());

Should it not be

if (!$conn) {
die('Could not connect: ' . mysql_error());
}

The { and } tells PHP which part of your script is if or while and which parts aren't.

1 Comment

In php, a single line (and only a single line) below an if/else statement does not require curly braces.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.