Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.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 如何在on SQL命令中使用两个where语句?_Php_Mysql - Fatal编程技术网

Php 如何在on SQL命令中使用两个where语句?

Php 如何在on SQL命令中使用两个where语句?,php,mysql,Php,Mysql,我有一些代码可以在两个日期之间选择数据库中的所有条目,但我也希望它不要在“状态”列中选择任何带有“已删除”的条目。我可以让它要么做,要么做,但不能同时做 try { $DBH = new PDO("mysql:host=localhost;dbname=$dbname",$dbuser,$dbpass); $DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $STH = $DBH->prepare("SELEC

我有一些代码可以在两个日期之间选择数据库中的所有条目,但我也希望它不要在“状态”列中选择任何带有“已删除”的条目。我可以让它要么做,要么做,但不能同时做

try {
$DBH = new PDO("mysql:host=localhost;dbname=$dbname",$dbuser,$dbpass);
$DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$STH = $DBH->prepare("SELECT Link, Type, Location, Urgency, Date, Time, Status, Days FROM reports WHERE truedate BETWEEN :start_date AND :end_date");
$STH->execute(array(':start_date' => $start_date_variable, ':end_date' => $end_date_variable));


$rows = $STH->fetchAll(PDO::FETCH_ASSOC);

echo "<table>";

foreach ($rows as $row) {
    echo "<tr><td>" . $row['Link'] . "</td><td>" . $row['Type'] . "</td><td>" . $row['Location'] . "</td><td>" . $row['Urgency'] . "</td><td>" . $row['Date'] . "</td><td>" . $row['Time'] . "</td><td>" . $row['Status'] . "</td><td>" . $row['Days'] . "</td></tr>";
}

echo "</table>";

}catch(PDOException $e) {
echo $e->getMessage();
}
试试看{
$DBH=newpdo(“mysql:host=localhost;dbname=$dbname”,$dbuser,$dbpass);
$DBH->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_异常);
$STH=$DBH->prepare(“从报告中选择链接、类型、位置、紧急程度、日期、时间、状态、天数,其中truedate介于:开始日期和:结束日期之间”);
$STH->execute(数组(':start\u date'=>$start\u date\u变量,':end\u date'=>$end\u date\u变量));
$rows=$STH->fetchAll(PDO::FETCH_ASSOC);
回声“;
foreach($行作为$行){
回显“$row['Link']”、$row['Type']”、$row['Location']”、$row['emergency']”、$row['Date']”、“$row['Time']”、“$row['Status']”、“$row['Days']”;
}
回声“;
}捕获(PDO$e){
echo$e->getMessage();
}

那么如何使用两个where语句来缩小结果范围呢?

只需在
truedate
检查之前使用
操作符即可,如图所示

SELECT Link, Type, Location, Urgency, Date, Time, Status, Days FROM reports WHERE Status NOT LIKE '%Removed%' AND truedate BETWEEN :start_date AND :end_date

只需在检查
truedate
之前使用
操作符,如图所示

SELECT Link, Type, Location, Urgency, Date, Time, Status, Days FROM reports WHERE Status NOT LIKE '%Removed%' AND truedate BETWEEN :start_date AND :end_date

试着用下面的代码更新你的代码

$STH = $DBH->prepare("SELECT Link, Type, Location, Urgency, Date, Time, Status, Days FROM reports WHERE Status != :statusval AND truedate BETWEEN :start_date AND :end_date");
$STH->execute(array(':statusval ' => 'Removed', ':start_date' => $start_date_variable, ':end_date' => $end_date_variable));

试着用下面的代码更新你的代码

$STH = $DBH->prepare("SELECT Link, Type, Location, Urgency, Date, Time, Status, Days FROM reports WHERE Status != :statusval AND truedate BETWEEN :start_date AND :end_date");
$STH->execute(array(':statusval ' => 'Removed', ':start_date' => $start_date_variable, ':end_date' => $end_date_variable));

在WHERE子句之后,添加一个AND子句和另一个条件。其中this=that和that=this在WHERE子句之后,添加一个AND子句和另一个条件。这里这个=那个,那个=这个