- <?php
- // 在这段脚本中,PHP变量$title 和 $price 分别为书名和价格上限的页面输入值。
- $sql = 'SELECT id,title,author,publisher,date,price FROM books';
- if($title !==''){//添加title查询条件(LIKE)
- $conditions[] = "title LIKE ? ESCAPE '#'";
- $ph_type[] ='text';
- $ph_value[]= esape_wildcard($title);
- }
- if($price !== ''){//添加price上限查询条件
- $conditions[] = "price <= ?";
- $ph_type[] ='integer';
- $ph_value[]= $price;
- }
- if(count($conditions) > 0){//存在where语句时
- $sql .= ' WHERE '.implode(' AND ',$conditions);
- }
- $stmt = $mdb2 ->prepare($sql , $ph_type);//准备SQL语句
- $rs = $stmt->excute($ph_value);//执行变量绑定和查询
- ?>
- //该片段来自于http://www.codesnippet.cn/detail/1301201614435.html
来源: http://www.codesnippet.cn/detail/1301201614435.html