0
我對數據庫運行查詢,並且必須檢查多個條件爲true。我所提供的代碼提取了我想要的數據,但是如果我必須檢查更多的列,它可能會很快失控。我不想要一個查詢,在那裏我必須寫出十幾個「AND」條件。我正在學習一些MySQL,希望得到任何幫助。尋找一個更清潔的方式來運行查詢,必須檢查多個「AND」條件
<?php
$servername = "localhost";
$username = "xxxx";
$password = "xxxx";
$dbname = "oldga740_SeniorProject";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//Set up and run my Query
$sql = "SELECT Project, Client, DateReceived, LastName, FinalReviewDate FROM Projects
WHERE FinalReviewDate IS NOT NULL
AND DateDelivered IS NULL
AND DateAccepted IS NULL
ORDER BY DateAccepted DESC";
$result = $conn->query($sql);
//Display results
if ($result->num_rows > 0) {
echo "<table><tr><th>Client</th><th>Project</th><th>Point of Contact</th><th>Date Project Received</th><th>Final Review Date</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["Client"]. "</td><td>" . $row ["Project"]. "</td><td> " . $row["LastName"]. "</td><td> " . $row ["DateReceived"]. "</td><td> " . $row["FinalReviewDate"]. "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
在代碼中我會放置數組?我假設它會替換WHERE和兩個AND語句... – Tony
您可以將數組放在$ sql變量上方。它取代了以下內容:FinalReviewDate IS NOT NULL AND DateDelivered IS NULL AND DateAccepted IS NULL –
我編輯了答案,查看最終代碼。 –