连接完数据库,在取值前加上
mysql_query("SET NAMES ***");
***要和你页面上的编码一样就行

解决方案 »

  1.   

    但是我加了之后,显示mysql_query("SET NAMES ***"); 这一句错误
      

  2.   

    假如你的页面设置的是UTF-8mysql_query("SET NAMES 'UTF-8'"); 
      

  3.   

    我的办法 全部地方都设成UTF-8
      

  4.   

    <html>
    <head> <title>Book-O_Rama Search Results</title>
    </head>
    <body>
    <h1>Book-O-Rama Search Results</h1>
    <?php
      //create short variable names
    $searchtype=$_POST['searchtype'];
    $searchterm=$_POST['searchterm'];
    $searchterm=trim($searchterm);
    if(!$searchtype||!$searchterm)
    {
    echo 'You have not entered search details. Please go back and try again.';
    exit;
    }


    if(!get_magic_quotes_gpc())
    {
    $searchtype=addslashes($searchtype);
    $searchterm=addslashes($searchterm);
    }

    @ $db=new mysqli('localhost','liuchao','liuchao','books');//分别连接的是本地主机、用户名、密码、数据库名

    if(mysqli_connect_errno())
    {
    echo 'Erro: Could not connect to database. Please try again later.';
    exit;
    }

    $query="select * from books where ".$searchtype." like '%".$searchterm."%'";//实际类型之间的匹配
    $result=$db->query($query);
       
    //$num_results=$result->num_rows;//对象
    $num_results=mysqli_num_rows($result);//过程式

    echo '<p>Number of books found:'.$num_results.'</p>';


    for($i=0;$i<$num_results;$i++)
    {
    $row=$result->fetch_assoc();
    echo '<p><strong>'.($i+1).'.Title:';
    echo htmlspecialchars(stripslashes($row['title']));
    echo '</strong><br/>Author:';
    echo stripslashes($row['author']);
    echo '<br/>ISBN:';
    echo stripslashes($row['isbn']);
    echo '<br/>Price:';
    echo stripslashes($row['price']);
    echo '</p>';
    }
    //$result->free();
    mysqli_free_result($result);

    $db->close();

    ?>
    </body>
    </html>
    这是我的代码,该怎么加啊,急
      

  5.   

    在$query="select * from books where ".$searchtype." like '%".$searchterm."%'";//实际类型之间的匹配
    上面写$db->query("SET NAMES 'UTF-8'")你的所有的文件都要存成UTF-8,页面设置的字符级也是UTF-8