Home >php教程 >PHP源码 >SQL查询动态占位符

SQL查询动态占位符

PHP中文网
PHP中文网Original
2016-05-26 08:18:141706browse

直接上代码

<?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 .= &#39; WHERE &#39;.implode(&#39; AND &#39;,$conditions);
}
$stmt = $mdb2 ->prepare($sql , $ph_type);//准备SQL语句
$rs = $stmt->excute($ph_value);//执行变量绑定和查询
 
?>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn