SQL查询动态占位符

2016-01-18 0 548
SQL查询动态占位符

       动态绑定举例

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

?>

遇见资源网 sql SQL查询动态占位符 http://www.ox520.com/17943.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务