以下是我的代码......
我正在尝试使用php pdo中的多个参数创建搜索....使用多个if else条件...在单个查询...
PLZ帮助......做到这一点......我怎么能用php pdo做到这一点......
<?php
$db=new PDO('mysql:host=localhost;dbname=circulation_scheme_prepaid','root','');
if(isset($_POST['Submit']))
{
$result=$db->prepare('SELECT * FROM receipt_entry WHERE');
if($_POST['book']!='')
{
$result->bindParam(':book',$_POST['book']);
}
$result->execute();
$data = $result->fetchAll();
}
?>
我想在上面编写代码..
<?php
require_once("includes/config.php");
if(isset($_POST['search']))
{
$sql = "SELECT * FROM properties WHERE";
if($_POST['location']!='')
{
$location = $_POST['location'];
$sql .= " location = '$location' AND";
}
if($_POST['purpose']!='')
{
$purpose = $_POST['purpose'];
$sql .= " purpose = '$purpose' AND";
}
$sql = substr($sql, 0 ,-3);
$query = mysql_query($sql);
while($row = mysql_fetch_array($query,MYSQL_ASSOC))
{
$rows[] = $row;
}
}
?>
答案 0 :(得分:0)
您可以尝试这样的事情:
if (isset($_POST['search'])) {
$sql = 'SELECT * FROM properties';
$where = array();
$params = array();
if (!empty($_POST['location'])) {
$where[] = "location = :location";
$params[':location'] = $_POST['location'];
}
if (!empty($_POST['purpose'])) {
$where[] = "purpose = :purpose";
$params[':purpose'] = $_POST['purpose'];
}
if(count($where) > 0)
$sql .= ' WHERE ' . implode(' AND ', $where);
$stmt = $db->prepare($sql);
foreach($params as $param => $value) {
$stmt->bindParam($param, $value);
}
$stmt->execute();
$data = $stmt->fetchAll();
print_r($data);
}