SQL查询动态占位符

 动态绑定举例

<?php // 在这段脚本中,PHP变量$title 和  $price 分别为书名和价格上限的页面输入值。   $sql = &#39;SELECT id,title,author,publisher,date,price FROM books&#39;;  if($title !==&#39;&#39;){//添加title查询条件(LIKE)      $conditions[] = "title LIKE ? ESCAPE &#39;#&#39;";      $ph_type[] =&#39;text&#39;;      $ph_value[]= esape_wildcard($title);  }  if($price !== &#39;&#39;){//添加price上限查询条件      $conditions[] = "price <= ?";      $ph_type[] =&#39;integer&#39;;      $ph_value[]= $price;  }  if(count($conditions) > 0){//存在where语句时      $sql .= ' WHERE '.implode(' AND ',$conditions);  }  $stmt = $mdb2 -&gt;prepare($sql , $ph_type);//准备sql语句  $rs = $stmt-&gt;excute($ph_value);//执行变量绑定和查询     ?&gt;
© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享