-1
votes

This PHP code does not work inside an HTML file but it works if its a PHP file

$con = mysql_connect("localhost","root","aaaa");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("maptemp", $con);


$sql = "SELECT * FROM users";
$rs = mysql_query($sql) or die(mysql_error());
$selectbox='<select name=\'userst\'>';
while ($row = mysql_fetch_assoc($rs)) {
$selectbox.='<option value=\"' . $row['username'] . '\">' . $row['username'] .  '</option>';
}
$selectbox.='</select>';
mysql_free_result($rs);
echo $selectbox;
1
HTML files don't usually run PHP scripts. Use .php.Wesley Murch
You must run a file with .php extensionSam
You need to configure the webserver to interpret HTML files as PHP.Bart
You mean if this code is in a file with the html extension?psu
@Bart Sure you can, but judging by the user's apparent knowledge I would say that may be bad starting advice.Wesley Murch

1 Answers

6
votes

Many (maybe most, all?) webservers aren't configured to look for php script inside html files out of the box.

Apache, a very popular webserver that you might be running, is an example of such a webserver.

You can, however, configure a webserver, like Apache, to do exactly what you want. If you can't access the Apache configuration files in the Apache directory itself (which is typical on shared servers, which, again, you most likely have), you can configure on a per-directory basis using .htaccess files.

Doing this is pretty simple: open up your favorite text editor, throw this line in there:

AddType application/x-httpd-php .html

and save it as .htaccess in the same folder as your .html file. And that should do it.

I'd advise against this, though, as people (like webservers) don't expect there to be php script inside an html file. It's good practice to just stick to .php files for your php script.