【问题标题】:Ajax search from mysql databse unicode failure从mysql数据库unicode失败的Ajax搜索
【发布时间】:2018-07-05 06:07:04
【问题描述】:

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

我有这段代码,用于从 MySQL 数据库表中搜索数据,它不读取 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/

我还向 HTML 文件添加了 utf-8 元数据。我希望有人告诉我出了什么问题。代码本身是:

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

【问题讨论】:

  • 代码也在网上你可以在这里查看link
  • 您在结果 div 中对 $_GET['terms'] 的输出是否正确显示?另外,您的 AJAX 代码在哪里?
  • @CBroe 我添加了 ajax 代码,是的,否则它会正确显示,但 Unicode 字符号除外。 this is online link for code你可以尝试输入“a”或“ä”来查看结果
  • 你因为没有正确编码你发送的数据而搞砸了。您不想使用escape,而是使用encodeURIComponent
  • 您请求的是dic.munjutut.fi/php/…,但它应该是dic.munjutut.fi/php/…ä 编码为%C3%A4

标签: php mysql unicode get


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2019-01-08
    • 1970-01-01
    • 2018-09-09
    • 2014-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-21
    • 1970-01-01
    相关资源
    最近更新 更多