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

SQL查询动态占位符

PHP中文网
PHP中文网Original
2016-05-26 08:18:141742Durchsuche

直接上代码

<?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);//执行变量绑定和查询
 
?>


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn