-2

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;
Peter Kiss
  • 9,309
  • 2
  • 23
  • 38
user2151530
  • 9
  • 1
  • 1
  • 3

1 Answers1

6

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.

Community
  • 1
  • 1
jamesplease
  • 12,547
  • 6
  • 47
  • 73