我有一個MySQL查詢,顯示來自我的數據庫的數據。以下是查詢:根據URL運行不同的MySQL查詢
$sql = "SELECT SUM(InvoiceAmount) AS TotalBilling,SUM(ClientCostToDate) AS WIP FROM Estimates WHERE date_format(InvoiceDate, '%Y-%m')=date_format(now(), '%Y-%m')";
此查詢將數據從MySQL數據庫顯示到我的頁面上的HTML表格中。我需要根據URL爲這個$sql
查詢添加一個小改動。這可能嗎?
理想的情況下,它的工作是這樣的:
如果/或index.php文件,運行如下命令:
$sql = "SELECT SUM(InvoiceAmount) AS TotalBilling,SUM(ClientCostToDate) AS WIP FROM Estimates WHERE date_format(InvoiceDate, '%Y-%m')=date_format(now(), '%Y-%m')";
如果/london.php,運行此:
$sql = "SELECT SUM(InvoiceAmount) AS TotalBilling,SUM(ClientCostToDate) AS WIP FROM Estimates WHERE Studio = 'London' date_format(InvoiceDate, '%Y-%m')=date_format(now(), '%Y-%m')";
有對每個頁面的查詢只是一個很小的變化 - 那就是顯示結果Studio = 'London'
。這樣我可以在london.php上顯示僅與倫敦有關的結果。
什麼是最好的方法來實現這一目標?感謝您的幫助。
這裏是我完整的腳本:
<?php
require_once 'config.php';
// create connection
$conn = new mysqli($currentConfig['host'], $currentConfig['user'], $currentConfig['pass'], $currentConfig['name']);
// check connection
if ($conn->connect_error) {
die('Connection failed: '.$conn->connect_error);
}
$sql = "SELECT SUM(InvoiceAmount) AS TotalBilling,SUM(ClientCostToDate) AS WIP FROM Estimates WHERE date_format(InvoiceDate, '%Y-%m')=date_format(now(), '%Y-%m') AND Studio = 'Splash London'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
$wip = $row['WIP'];
$total_billing = $row['TotalBilling'];
// US national money format
setlocale(LC_MONETARY, 'en_US');
// echo data into HTML table
echo
'<tbody>'.'<tr>'.'<td>'. money_format('%(#10n',$total_billing) . "\n" .'</td>'.'<td>'. money_format('%(#10n',$wip) . "\n" .'</td>';
}
} else {
echo 'No results';
}
$conn->close();
只是做一個if語句 – rak007