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

时间:2018-07-05 06:07:05

标签: php mysql unicode get

我希望主持人不要将此问题标记为重复的原因,因为我将在近一个星期的时间内对此问题进行研究,并且不能弄清楚出了什么问题。

我有这段代码,用于从MySQL数据库表中搜索数据,atm不会读取Unicode字符。我的猜测是$ _GET函数字符串读取Unicode字符,因此,我将MySQL表字符集设置为utf-8,列排序规则为utf8_general_ci,并且还将这两行添加到php代码中:

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

但是Unicode字符的结果仍然是0。如果我在输入中键入“ a”,它将显示以“ a”或“ä,åá...”开头的所有单词,但是,如果我键入一些Unicode, char例如“ä”,它表示“没有结果”。

代码也在线atm,这是链接http://dic.munjutut.fi/php/

我还已经将utf-8 meta添加到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>
';

2个文件中的Ajax代码:

    search_id = '';
  function handleHttpResponse() {
        if (http.readyState == 4) {
            if (search_id != '') {
                document.getElementById(search_id).innerHTML = http.responseText;
            }
        }
    }
  function getHTTPObject() {
        var xmlhttp;
        if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
            try {
                xmlhttp = new XMLHttpRequest();
            } catch (e) {
                xmlhttp = false;
            }
        }
        return xmlhttp;
    }
    var http = getHTTPObject();

  function getScriptPage(div_id,terms_id,get_count,get_p) {
    search_id = div_id;
     zearch = document.getElementById(terms_id).value;
    http.open("GET", "search.php?terms=" + escape(zearch)+"&count="+get_count+"&page="+get_p, true);
   http.onreadystatechange = handleHttpResponse;
  http.send(null);
    }

function GetXmlHttpObject(handler)
{
  var objXMLHttp=null
  if (window.XMLHttpRequest)
  {
      objXMLHttp=new XMLHttpRequest()
  }
  else if (window.ActiveXObject)
  {
      objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
  }
  return objXMLHttp
}

function stateChanged()
{
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
  {
          // show_results will be filled with new page
          document.getElementById("show_results").innerHTML = xmlHttp.responseText;
          document.getElementById("show_results").scrollIntoView();
  }
}

function htmlData(url, terms, pag)
{
  if (url.length==0)
  {
      document.getElementById("show_results").innerHTML = "";
      return;
  }

  xmlHttp = GetXmlHttpObject();

  if (xmlHttp==null)
  {
      alert ("Browser does not support HTTP Request");
      return;
  }

  url=url+"?"+terms+"&amp;"+pag;
  url=url+"&sid="+Math.random();
  xmlHttp.onreadystatechange = stateChanged;
  xmlHttp.open("GET",url,true) ;
  xmlHttp.send(null);
}

1 个答案:

答案 0 :(得分:0)

@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);