James
James

Reputation: 15

php script error within html file

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!

Upvotes: 0

Views: 70

Answers (3)

kylex
kylex

Reputation: 14406

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.

Upvotes: 2

Mattios550
Mattios550

Reputation: 372

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.

Upvotes: 0

ddavison
ddavison

Reputation: 29032

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.

Upvotes: 0

Related Questions