+ -
当前位置:首页 → 问答吧 → PHP的BUG? preg_replace后导致查询语句失效问题

PHP的BUG? preg_replace后导致查询语句失效问题

时间:2011-12-25

来源:互联网

以下PHP代码里的mysql查询为何得不到结果?
$a = 'Gifts & Party Supplies';
$a = strval(preg_replace("/[[:punct:]]/","",strtolower($a)));// 把&等标点符号去掉,并转为小写 $a='gifts party supplies';
$master_category_id_sql = "select categories_id from categories_description where lower(categories_name)='". $a ."' ";
echo $master_category_id_sql;//查询语句是: select categories_id from categories_description where lower(categories_name)='gifts party supplies' 
$master_category_id_q = mysql_query($master_category_id_sql);
$master_category_id_r = mysql_fetch_array($master_category_id_q);
$master_category_id = $master_category_id_r['categories_id'];//产品目录ID
var_dump($master_category_id);//null
我发现php代码中多了那一句: preg_replace("/[[:punct:]]/","",strtolower($a)) 后, 后面的sql查询就得不到结果.
我直接把select categories_id from categories_description where lower(categories_name)='gifts party supplies'这句放到mysql里查询是可以得到结果的.
或者我把这行代码 $master_category_id_sql = "select categories_id from categories_description where lower(categories_name)='". $a ."' "; 里的变量$a去掉, 写成$master_category_id_sql = "select categories_id from categories_description where lower(categories_name)='gifts party supplies' "; 也是可以得到结果的.

问题就出在preg_replace("/[[:punct:]]/","",strtolower($a))这行.
百思不得其解. 麻烦大家解析一下.

作者: jimx92   发布时间: 2011-12-25

$a = 'Gifts & Party Supplies';
echo $a = strval(preg_replace("/[[:punct:]]/","",strtolower($a)));// 把&等标点符号去掉,

得到的是 gifts party supplies 注意:gifts 后面有两个空格
而你测试成功的是 gifts party supplies 注意:gifts 后面只有一个空格

这就不难理解了吧

作者: xuzuning   发布时间: 2011-12-25