douao3063 2013-12-21 15:51
浏览 38

显示基于URL PDO的结果

I am fetching results from database using PDO with url query string

url: index.php?bType=doctor&loc=asia|india

CODE:

$bind = array();
$str=explode('|',$_GET[loc]); 
  foreach ($str as $loc) 
{
    $bloc[] = $loc;
}   $loca = implode("','", $bloc); 
 $btype=$_GET['bType'];

$sqlsb = "SELECT * FROM t_business WHERE 1=1";
if(isset($_GET['bType'])){ $sqlsb .=" AND type like :btype"; $bind['btype']='%'. $btype .'%';}

if(isset($_GET['loc'])){ $sqlsb .=" AND location IN (:loca)"; $bind['loca']="'$loca'";}

$qsb = $db->prepare($sqlsb);
$qsb->execute($bind);

above code fetches nothing..

print_r($bind) shows Array ( [btype] => doctor [loca] => asia','india )

If i do it without using prepared it works.

I am writing this code for refine search filters.

Thanks

UPDATE

AS CoursesWeb anwserd i did changes

if(isset($_GET['bType'])){ $sqlsb .=" AND type like :btype"; $bind['btype']='%'. $btype .'%';}
if(isset($_GET['loc'])){ $sqlsb .=" AND location IN (:loca)";  $bind['loca']="'$loca'";}

it works for :btype but not with :loca

print_r($bind) shows array ( [btype] => %doctor% [loca] => 'asia','india' )

Need to do something better with loc than using implode..think so

  • 写回答

1条回答 默认 最新

  • dongnaota6386 2013-12-21 16:01
    关注

    for prepared values used in LIKE statement, the "?" or "%" characters must be added together with the value, not in sql statement.

    $btype = '%'. $btype .'%';
    $sqlsb = "SELECT * FROM t_business WHERE type LIKE :btype";
    
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么