Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从mysql数据库unicode进行Ajax搜索失败_Php_Mysql_Unicode_Get - Fatal编程技术网

Php 从mysql数据库unicode进行Ajax搜索失败

Php 从mysql数据库unicode进行Ajax搜索失败,php,mysql,unicode,get,Php,Mysql,Unicode,Get,我希望版主不要将这个问题标记为重复的问题,因为我对这个问题进行了将近1周的研究,无法找出问题所在 我有这段代码,用于从MySQL数据库表中搜索数据,它不读取Unicode字符。我的猜测是$\u GET function string读取Unicode字符,因此,我将MySQL表字符集设置为utf-8,列排序规则为utf8\u general\u ci,并将这两行添加到php代码中: header('Content-Type: text/html; charset=utf-8'); mysqli_

我希望版主不要将这个问题标记为重复的问题,因为我对这个问题进行了将近1周的研究,无法找出问题所在

我有这段代码,用于从MySQL数据库表中搜索数据,它不读取Unicode字符。我的猜测是$\u GET function string读取Unicode字符,因此,我将MySQL表字符集设置为utf-8,列排序规则为utf8\u general\u ci,并将这两行添加到php代码中:

header('Content-Type: text/html; charset=utf-8');
mysqli_set_charset($connect,'utf8');
但Unicode字符的结果仍然是0。如果我在输入中键入“a”,它会显示以“a”或“ä,åá…”开头的所有单词,但是,如果我键入一些Unicode字符,例如“ä”,它会显示“未找到结果”

代码也在线atm这里是链接

我还将utf-8元数据添加到HTML文件中。我希望有人告诉我怎么了。守则本身是:

header('Content-Type: text/html; charset=utf-8');
mysqli_set_charset($connect,'utf8');

if(isset($_GET['p'])) {
  $page_number = $_GET['p'];
  $arraySearch = $_GET['terms'];
  $show_count = $_GET['count'];
  settype($page_number, 'integer');
}
    $nospaces = substr($_GET['terms'],1,4);

    $offset = ($page_number - 1) * $records_number;
// check for an empty string and display a message.
if ($_GET['terms'] == "") {
  echo  '<div id="counter">Type "äää" or "ääää"!</div>';
// minim 3 characters condition
  } else if(strlen($_GET['terms']) < $limitchar) {
 echo '<div id="counter">'. $limitchar .' characters minimum</div>';

  } else  {

// explode search words into an array
  $arraySearch = explode(" ", $_GET['terms']);
// table fields to search
  $arrayFields = array(0 => $first_field, 1 => $second_field);
  $countSearch = count($arraySearch);
  $a = 0;
  $b = 0;
  $query = "SELECT * FROM $table_name WHERE (";
  $countFields = count($arrayFields);
  while ($a < $countFields)
  {
    while ($b < $countSearch)
    {
      $query = $query."$arrayFields[$a] LIKE '$arraySearch[$b]%'";
      $b++;
      if ($b < $countSearch)
      {
        $query = $query." AND ";
      }
    }
    $b = 0;
    $a++;
    if ($a < $countFields)
    {
      $query = $query.") OR (";
    }
  }
  $query = $query.") LIMIT $offset, $records_number;";
  $search = mysqli_query($connect, $query);


// get number of search results
  $arrayFields = array(0 => $first_field);
  $countSearch = count($arraySearch);
  $a = 0;
  $b = 0;
  $query = "SELECT * FROM $table_name WHERE (";
  $countFields = count($arrayFields);
  while ($a < $countFields)
  {
    while ($b < $countSearch)
    {
      $query = $query."$arrayFields[$a] LIKE '%$arraySearch[$b]%'";
      $b++;
      if ($b < $countSearch)
      {
        $query = $query." AND ";
      }
    }
    $b = 0;
    $a++;
    if ($a < $countFields)
    {
      $query = $query.") OR (";
    }
  }
  $query = $query.")";
  $count_results = mysqli_query($connect, $query) or die(mysqli_error($connect));

  $numrows = mysqli_num_rows($count_results);

// no results
if($numrows == 0) {
        echo '<div id="counter">No results found</div>';

// show results
} else {

echo '<div id="results">
<div id="results_top"><p><b>'. $_GET['terms'] .'</b> - '. $numrows .' results found</p></div>
';


@CBroe帮我修好了!非常感谢他!问题在于AJAX和编码。我使用了
escape
函数代替
encodeURIComponent
。这里是固定代码:

旧的:

http.open("GET", "search.php?terms=" + escape(zearch)+"&count="+get_count+"&page="+get_p, true);
固定的:

http.open("GET", "search.php?terms=" + encodeURIComponent(zearch)+"&count="+get_count+"&page="+get_p, true);

代码也是在线的。你可以在这里查看你在results div中对$\u GET['terms']的输出,是否正确显示?另外,您的AJAX代码在哪里?@CBroe我添加了AJAX代码,除Unicode字符外,其他代码都显示正确。您可以尝试键入“a”或“ä”查看结果。您发送的数据编码不正确,这会把结果搞砸。您不想使用
转义
,但要使用
编码组件
。您正在请求,但它应该是(
a
编码为
%C3%A4
http.open("GET", "search.php?terms=" + escape(zearch)+"&count="+get_count+"&page="+get_p, true);
http.open("GET", "search.php?terms=" + encodeURIComponent(zearch)+"&count="+get_count+"&page="+get_p, true);