dongzong8110 2013-03-15 03:20
浏览 144
已采纳

在mysql查询LIKE中使用#%_ +进行搜索

I have this code ( just some part of it) :

<?php
if(isset($_GET['act']) && $_GET['act']== 'do')  {
$key= $_POST['key'];
}
else {
$key= '';
}
?>

<input class='inputField' type='text' name='key' size=45 value='<?php echo $key;?>' > 

<script>$(document).ready(function(){
key= $("#mainTop input").val();
if(key!= null || key== '%') {
    showPage(1,key);
}
else    {
    showPage(1);
}
});
</script>

The showpage() will get parameter and pass to php file :

if(isset($_GET['key'])) {
$key= addslashes($_GET['key']);
}
else {
$key= null;
}
$result= $db->query("SELECT * FROM information WHERE stuId LIKE '%$key%' OR stuName LIKE '%$key%' LIMIT  $start,10");

The php file will get the key to search. I escape the key by addslashes,the do the mysql query to search. But if I search with key = % # _ +,it still print our all the table instead there are no matching result. I guess that I did not escape the key in right way,so how do I escape character such as % # _ + to do mysql query with LIKE ? Please help me out ? I had a look at some another question but I still dont get it

  • 写回答

2条回答 默认 最新

  • dongyou2305 2013-03-15 03:23
    关注

    As far as I know, # and + are not special characters in a LIKE match string, but you simply need to escape the others

    $key = str_replace(array('%', '_'), array('\%', '\_'), $key);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度