SELECT语句不适用于utf-8

时间:2015-10-18 18:53:16

标签: php utf-8 mysqli

我有一个select语句,它应该选择数据库中与阿拉伯语输入相等的所有数据,我将数据库的排序规则设置为utf8_general_ci,并将table列设置为。并在文件

$conn = mysqli_connect($mysql_host, $mysql_user, $mysql_password, $mysql_database); 
mysqli_query($conn, "set names 'utf8'");
mysqli_query($conn, "SET character_set_results=utf8");
mb_http_output('UTF-8'); 
mb_internal_encoding('UTF-8');
mb_http_input('UTF-8');
mb_language('uni');
mb_regex_encoding('UTF-8');
ob_start('mb_output_handler');
mb_language('uni'); 
mb_internal_encoding('UTF-8');
mysqli_set_charset($conn, "utf8");

搜索代码

$question = $_REQUEST['question'];
$table = $_REQUEST['subject'];
if(empty($question)) {
echo "Type a question!"; }
$qe = mysqli_escape_string($conn, $question);
$full = "SELECT * FROM $table WHERE question LIKE '%$qe%'";
$fullQ = mysqli_query($conn, $full);
if(!$fullQ) {
echo mysqli_error($conn);
}
echo "<meta http-equiv='Content-Type' content='text/html;charset=UTF-8'>";
printf(nl2br("\n%u\n"), mysqli_num_rows($fullQ));
while($row = mysqli_fetch_assoc($fullQ)) {
printf(nl2br("\n%s\n"), $row['question']);
}

如果我在不使用WHERE的情况下打印所有数据,它也会起作用,并且在使用正确的阿拉伯字符从第一页发送到第二页后,它也会打印SQL语法。但它总是返回0行。

1 个答案:

答案 0 :(得分:0)

如果您的数据库以阿拉伯语保存,则将连接字符串设置为以下

$con1 = mysqli_connect($HOSTURL,$DBUSER,$DBPASSWORD,$DBNAME);

    // Check connection
        if (mysqli_connect_errno())
          {
          echo "Failed to connect to MySQL: " . mysqli_connect_error();
            die('');
          }
        mysqli_query($con1,"SET NAMES utf8");   
      //else{echo "Connection Done";}

如果您的数据库没有编码,那么您不必在连接字符串中设置utf8。