- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我希望版主不要将此问题标记为重复,因为我研究这个问题近 1 周,但无法找出问题所在。
我有这段代码,用于从 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字符,例如“ä”,它表示“未找到结果”。
代码也是在线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+"&"+pag;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange = stateChanged;
xmlHttp.open("GET",url,true) ;
xmlHttp.send(null);
}
最佳答案
@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);
关于php - Ajax从mysql数据库unicode搜索失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51184350/
我还没有找到太多关于何时使用 Unicode 的(简明)信息。我知道很多人说最佳实践是始终使用 Unicode。但 Unicode 字符串确实有更多的内存占用。我是否正确地说,必须仅在以下情况下使用
我正在构建一个需要使用表情符号的应用程序,特别是生成大量随机表情符号序列。这需要有一个大列表可供选择。而不是采取方法 detailed here通过循环硬编码十六进制范围,我决定采用不同的方法并从 t
早在 ZX Spectrum 的早期,就有一种方法可以将一个字形打印在另一个字形之上,从而在 OVER 1 指令的帮助下创建复合字形。 我想知道是否有 Unicode 方法可以在现代计算机上执行相同的
我有一个表示 Unicode 代码点的字符串,例如 "272d"。如何将其转换为 "✭"? Elixir 当然理解 Unicode: iex> > "✭" iex> "x{272d}" "✭" 但我需
自从我了解到 clang 能够编译用 Unicode 编写的 c++ 源文件后,我在编写与数学相关的代码时就开始大量使用它。比较 uₙ₊₁ᵖ = A*uₙ + B*uₙ₋₁; uₙ₊₁ᶜ = π *
感谢jmcnamara我发现了一种在 xlsxwriter 图表中使用 Unicode 字符的好方法:xlsxwrter: rich text format in chart title 我需要一个所
有些字符不包含在 Unicode 中(即带重音的西里尔字母),但可以使用组合序列创建。据我了解,可能的组合字符序列是在布局引擎和/或使用的字体中定义的。我对吗?那么,如何得到所有可能的组合序列呢? 最
我正在尝试使用 libunibreak ( https://github.com/adah1972/libunibreak ) 来标记某些给定 unicode 文本中可能的换行符。 Libunibre
我需要具有属性 Alphabetic 的 Unicode 字符范围列表如 http://www.unicode.org/Public/5.1.0/ucd/UCD.html#Alphabetic 中所定
我想为 Unicode 中的特定字符找到视觉上相同的字符。 我知道如何找到一个字符的规范或兼容性分解;但他们没有给我我想要的。 我想找到视觉上相同(不相似)的字符,它们唯一的区别可能是它们的大小。 例
假设我有包含此字符串的 Apache Solr 索引文档: Klüft skräms inför 我希望能够使用此关键字通过搜索找到它(注意“u”-“ü”): kluft 有没有办法做到这一点 ? 最
我已经阅读了很多文章以了解 Unicode 代码点的最大数量,但我没有找到最终答案。 我知道 Unicode 代码点已最小化,以使所有 UTF-8 UTF-16 和 UTF-32 编码都能够处理相同数
我正在使用 CSS Buttons With Icons But No Images . 图标是使用 unicode 值生成的。在这方面,我遇到了一些浏览器不支持某些 unicode 值的问题。因此,
我正在寻找一种方法将 Unicode 字母字符从任何语言音译为带重音的拉丁字母。目的是让外国人深入了解以任何非拉丁文字书写的姓名和单词的发音。 例子: 希腊语:Romanize("Αλφαβητικό
Unicode 6.0 添加了几个带有描述的字符,表明这些字符应该以特定颜色呈现: 红苹果 U+1F34E 青苹果 U+1F34F 蓝心U+1F499 绿心U+1F49A 黄心U+1F49B 紫心U+
我想知道,Unicode 中的每个字符都有一个代码点;字体中字符的类似术语是什么? 当解码文件需要映射到字体(或字体,通过一些现代字体替换技术)时,我从来没有理解过程的一部分。 例如,当文本编辑器从其
谁能告诉我 Unicode 可打印字符的范围是多少? [例如。 Ascii 可打印字符范围为\u0020 -\u007f] 最佳答案 参见,http://en.wikipedia.org/wiki/U
鉴于Unicode有been around for 18 years ,为什么还有不支持 Unicode 的应用程序?甚至我对某些操作系统和 Unicode 的体验至少可以说是痛苦的。正如乔尔·斯波尔
我要求计算 Unicode 中所有可能的有效组合的数量并附上解释。我知道一个 char 可以编码为 1、2、3 或 4 个字节。我也不明白为什么连续字节有限制,即使该字符的起始字节清除了它应该有多长。
Unicode 为中文字符分配了 U+4E00..U+9FFF。这是全套的一部分,但不是全部。 最佳答案 最终列表可以在 Unicode Character Code Charts 找到;在页面中搜索
我是一名优秀的程序员,十分优秀!