Home > Database > Mysql Tutorial > SQL query dynamic placeholder

SQL query dynamic placeholder

大家讲道理
Release: 2016-11-10 11:33:20
Original
1211 people have browsed it

Dynamic binding example

<?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);//执行变量绑定和查询
 
?>
Copy after login
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template