浏览器显示“???”而不是UTF-8字符。原因是什么?如何解决?
这是HTML文件:
<HTML>
<title>Search Engine</title>
<form action='search.php' method='GET'>
<font face='sans-serif' size='5'>
<center>
My Search Engine.<br>
<input type='text' size='50' name='search'> <input type='submit' name='submit' value='Search'><br>
</center>
</font>
<center><a href='submiturl.php'>Submit a URL</a></center>
</form>
</HTML>
这是search.php
:
<?php
//get data
$button = $_GET['submit'];
$search = $_GET['search'];
if (!$button)
echo "Please fill out the form";
else
{
if (strlen($search)<=2)
echo "The item you searched for was to small";
else
{
echo "You searched for <b>$search</b> <hr size='1'>";
//connect to database
mysql_connect('localhost','k_search','1234.');
mysql_select_db('k_search');
//explode search term
$search_exploded = explode(" ",$search);
foreach($search_exploded as $search_each)
{
//construct query
$x++;
if ($x==1)
$construct .= "keywords LIKE '%$search_each%'";
else
$construct .= " OR keywords LIKE '%$search_each%'";
}
//echo out construct
$construct = "SELECT * FROM searchengine WHERE $construct";
$run = mysql_query($construct);
$foundnum = mysql_num_rows($run);
if ($foundnum==0)
echo "No results found.";
else
{
echo "$foundnum results found.<p><hr size='1'>";
while ($runrows = mysql_fetch_assoc($run))
{
//get data
$title = $runrows['title'];
$desc = $runrows['description'];
$url = $runrows['url'];
echo "<b>$title</b><br>
$desc<br>
<a href='$url'>$url</a><p>";
}
}
}
}
?>
这是submiturl.php
:
<HTML>
<title>Search Engine</title>
<form action='submiturl.php' method='POST'>
<font face='sans-serif' size='5'>
<center>
Please fill out all fields to submit your URL.<br><br></font>
URL:<br><input type='text' size='50' name='url' value='http://www.'><br><br>
Site Name:<br><input type='text' size='50' name='title'><br><br>
Description (max 200 characters):<br><input type='text' size='50' name='description' maxlength='200'><br><br>
Keywords:<br><input type='text' size='50' name='keywords'><br><br>
<input type='submit' name='submit' value='Submit URL'>
</form>
<br><a href='index.html'>Go Back</a>
</center>
</HTML>
<?php
$submit = $_POST['submit'];
$title = $_POST['title'];
$description = $_POST['description'];
$url = $_POST['url'];
$keywords = $_POST['keywords'];
$connect = mysql_connect("localhost","k_search","1234.");
mysql_select_db("k_search");
if (!$title||!$description||!$url||!$keywords)
{
die ("<center>Please fill in all fields.</center>");
}
else
if ($submit)
{
mysql_query("INSERT INTO searchengine VALUES('','$title','$description','$url','$keywords')");
echo "<center>Website Submitted!</center>";
}
else
echo "<center>Please fill in all fields.</center>";
?>
答案 0 :(得分:3)
这可能有两个原因:
INSERT
期间使用UTF-8。要修复其中一个,请阅读:PHP UTF-8 cheatsheet。
也就是说,您的代码中还存在其他问题。自1998年以来,<font>
和<center>
代码已弃用。Use CSS。此外,SQL对SQL注入攻击很敏感。 Sanitize您的SQL参数。
答案 1 :(得分:2)
我曾经挣扎过“????????”人物,但我得到了答案。
尝试为PHP文件使用没有BOM编码的UTF-8。 Notepad ++可以处理这种类型的文件编码。
在Notepad ++中,只需转到编码,然后检查没有BOM的UTF-8。
答案 2 :(得分:1)
如果db在utf-8-general-ci那么你必须将php文件编码为UTF-8
这适合我。
答案 3 :(得分:0)
检查你是否声明了doc类型和字符集,这通常是原因..
答案 4 :(得分:0)
我认为问题在于您的“已设置”表格排序。
您必须为此编辑表格中的每个字段:(
http://www.assistprogramming.com/wp-content/uploads/2007/09/table.jpg