views:

122

answers:

3

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.'&gt;&lt;br&gt;&lt;br&gt;
            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>";
?>
+3  A: 

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.

BalusC
wel my script working f9 but my problm is that.. 1st.i Should put other language when i type n submit other languge so submit are f9 but when i m searching keyword so my webpage show "???" like..
salman
Sorry, I find it hard to understand you. Can you please rephrase your comment in proper English?
BalusC
salman
I'd suggest to learn PHP (and HTML) the proper way. Don't copypaste other's code, but write it yourself. Unfortunately, in the PHP world the *real good* tutorials are [extremely rare](http://stackoverflow.com/questions/2119083/php-tutorial-that-is-security-accuracy-and-maintainability-conscious). At least, the answer to **this particular problem** is already described in the [PHP UTF-8 cheatsheet](http://developer.loftdigital.com/blog/php-utf-8-cheatsheet) link. If you can't/don't understand it, then you should be more specific in describing what exactly you didn't understand from it.
BalusC
A: 

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

Haroldo
No, this would rather have caused [Mojibake](http://en.wikipedia.org/wiki/Mojibake), not `???`. Btw: doctype is irrelevant to the character encoding problem. Although I fully agree that a proper one should be added to ensure predictable browser behaviour with regard to HTML/CSS rendering.
BalusC
A: 

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

jasimmk