开发者

The browser shows me "???" instead of UTF-8 characters

The browser shows me "???" instead of UTF-8 characters. What is the cause and how can I fix it?

Here is the HTML file:

<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>

This is the 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>";

       }

     }



    }
}


?>

This is the 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 (开发者_如何学C!$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>";
?>


This can have 2 causes:

  1. The DB wasn't instructed to use UTF-8 during INSERT.
  2. PHP wasn't instructed to use UTF-8 during echoing output.

To fix the one or other, read this: PHP UTF-8 cheatsheet.


That said, there are another problems in your code as well. The <font> and <center> tags are deprecated since 1998. Use CSS. Further the SQL is sensitive to SQL injection attacks. Sanitize your SQL parameters.


I used to struggle with "????????" characters, but I got the answer for me.

Try to use UTF-8 without BOM encoding for your PHP file. Notepad++ can handle this type of file encoding.

In Notepad++, just go to the Encoding then check UTF-8 without BOM.


If db is in utf-8-general-ci THEN you must set the php file Encoding to UTF-8

That works for me.


check you have declared a doc type and character set, this is often the cause..


I think the problem is with your "already set" table collation.

  1. Ensure that collation for the table is set to utf8_general_ci.
  2. Ensure that collation for every field in table is set to utf8_general_ci

you have to edit every field in the table for this :(

http://www.assistprogramming.com/wp-content/uploads/2007/09/table.jpg

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜