Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 和运算符忽略空值_Php_Mysql - Fatal编程技术网

Php 和运算符忽略空值

Php 和运算符忽略空值,php,mysql,Php,Mysql,我有一个包含不同字段(如设施、类型等)的表。我有一个包含类型、设施字段和搜索按钮的搜索表单。当我输入两个字段时,它会给出结果,但当我只输入一个字段时,它不会显示任何输入。我在Where子句中使用and运算符 $facility=$_POST[facility]; $user=$_POST[user]; $type=$_POST[type]; $sql = "SELECT * FROM tblfacility join tbluser on tbluser.id=tblfacility.user

我有一个包含不同字段(如设施、类型等)的表。我有一个包含类型、设施字段和搜索按钮的搜索表单。当我输入两个字段时,它会给出结果,但当我只输入一个字段时,它不会显示任何输入。我在Where子句中使用and运算符

$facility=$_POST[facility];
$user=$_POST[user];
$type=$_POST[type];
$sql = "SELECT * FROM tblfacility join tbluser on tbluser.id=tblfacility.user where facility = '".$facility."' AND type= '".$type."' " ;
$query = $dbh -> prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
//echo "<prep>";
//echo "this is the final";
//print_r($row);
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $result)
$facility=$\u POST[facility];
$user=$_POST[user];
$type=$_POST[type];
$sql=“SELECT*FROM TBLFACLITY join tbluser on tbluser.id=TBLFACLITY.user,其中facility='”“$facility.”和type='“$type.”;
$query=$dbh->prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
//回声“;
//回声“这是最后的”;
//打印(行);
$cnt=1;
如果($query->rowCount()>0)
{
foreach($results作为$result)
我认为它是把空值作为一个值,然后进行and运算并给出结果。我怎么能在and运算中忽略空值呢

$wheres = [];
if (!empty($_POST['facility'])) {
    $wheres[] = "facility = '" . $_POST['facility'] . "'";
}
if (!empty($_POST['type'])) {
    $wheres[] = "type= '" . $_POST['type'] . "'";
}

$sql = "SELECT * FROM tblfacility join tbluser on tbluser.id=tblfacility.user";
if ($wheres) {
    $sql .= ' WHERE ' . implode(' AND ', $wheres);
}
$query = $dbh -> prepare($sql);

另外,如果查询中没有占位符,准备查询是没有用的。

我想你在
类型
前面漏掉了一个
$
。1.这里没有JavaScript 2.这里没有HTML 3.这里没有AND here-SQL WHERE子句使用OR。你不会得到任何结果,这意味着没有匹配的空类型或在您的计算机上清空设备table@KIKOSoftware感谢我纠正了仍然不起作用的错误。当心,这段代码对SQL注入非常开放:您正在连接用户输入并将它们直接发送到数据库。