PHP不处理HTML文件 [英] PHP not working on HTML file
问题描述
这个PHP代码在HTML文件中不起作用,但是如果它的PHP文件是有效的的话。
本地主机, 根, AAAA);if(!$ con)
{
die('Could not connect:'。mysql_error());
}
mysql_select_db(maptemp,$ con);
$ sql =SELECT * FROM users;
$ rs = mysql_query($ sql)或死(mysql_error());
$ selectbox ='< select name = \'userst\'>';
while($ row = mysql_fetch_assoc($ rs)){
$ selectbox。='< option value = \''。$ row ['username']。'\>' 。 $ row ['username']。 < /选项>;
}
$ selectbox。='< / select>';
mysql_free_result($ rs);
echo $ selectbox;
t被配置为在 html
文件中寻找 php
脚本。
Apache是一款非常流行的网络服务器,它可以帮助您轻松地管理您的网站。,这是一个这样的web服务器的例子。
然而,你可以配置一个像Apache一样的网络服务器,可以完全按照你的意愿去做。如果您无法访问Apache目录本身的Apache配置文件(这在共享服务器上很常见),那么您可以使用 .htaccess
文件。
这样做很简单:打开你最喜欢的文本编辑器,把这一行放在那里:
AddType application / x-httpd-php .html
并保存为 .htaccess
放在与 .html
文件相同的文件夹中。这应该做到这一点。
不过,我建议不要这样做,因为人们(比如web服务器)不希望有 php
脚本。对于 html
文件中的 php
脚本,坚持 .php
文件是一种很好的做法。
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;
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.
这篇关于PHP不处理HTML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!