test.html点击查询后,调用test.php,需要查询的字符也通过url编码的方式,发送到test.php,然后mysqli调用sql语句。
过程很简单,代码也来了,可以查询英文,汉字处理不了。
下面是test.html
$(function(){
$("#Submit").click(function(){
var key = escape($("#key").val());
var sel = $("#sel").val();
if( key != 0 )
{
$.get("test.php",{key:key,sel:sel,sid:Math.random()},
function(data){
$("#disp").html(data);
});
}
else
{
$("#disp").html("搜索内容不为空");
}
});
});
</script>
<h2 align="center" style="margin:4px">以ajax方式查询数据</h2>
<div style="border:1px solid gray;background:#eee;padding:4px">
查询留言:请输入关键字
<input id="key" type="text">
<select id="sel" name="">
<option value="title">文章标题</option>
<option value="content">文章内容</option>
</select>
<input id="Submit" type="button" value="查询">
</div>
<div id="disp"></div>
</body>
</html>
<div id="disp"></div>
test.php部分如下
<?php
header("Content-type:text/html;charset=utf-8");
ini_set('display_errors', 1);
error_reporting(E_ALL);
include("conn.php");
$key = trim($_GET["key"]);
$sel = $_GET["sel"];
$sql = "select * from lyb";
if($key != ""){
$sql = $sql." where $sel like '%$key%';";
$result = $conn->query($sql);
}
if($result->num_rows > 0){ ?>
<p>关键字为<?php echo $key ?>,共找到<?php echo $result->num_rows ?>条留言</p>
<table>
<tr>
<th>标题</th>
<th>内容</th>
<th>作者</th>
<th>email</th>
<th>ip</th>
</tr>
<?php while($row = $result -> fetch_assoc()){ ?>
<tr>
<td><?php echo $row['title'] ?></td>
<td><?php echo $row['content'] ?></td>
<td><?php echo $row['author'] ?></td>
<td><?php echo $row['email'] ?></td>
<td><?php echo $row['ip'] ?></td>
</tr>
<?php }?>
</table>
<?php }
else
echo "<p>没有搜索到任何留言</p>";
?>
查询英文
查询中文(ajax)
直接通过数据调用
select title,content from lyb where title like '%大家%';
+-----------------+-----------------------------------------+
| title | content |
+-----------------+-----------------------------------------+
| 祝大家开心 | 非常感谢大家长期以来的帮助 |
| 祝大家开心 | 非常感谢大家的帮助 |
| 大家好! | 雪花纷飞 |
| 大家好呀 | 下雪了 |
| 大家好 | 一起讨论 |
| 大家好 | utkj混口饭大公开法国队 |
我考虑了,应当是test.php中,这里的问题
$key = trim($_GET["key"]);
修改成
$key = unescape(trim($_GET["key"]));
运行报错 Call to undefined function unescape()
请问,如何解决?
你需要encodeURI一下中文参数