2016-09-16 28 views
0

我有一個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(); 
+0

只是做一個if語句 – rak007

回答

1

也許你可以使用$_SERVER['REQUEST_URI']

例如,如果你完整鏈接爲:http://www.mypage.com/index.php

$_SERVER[HTTP_HOST]有 「www.mypage.com」

$_SERVER[REQUEST_URI]有 「的index.php」

所以,你可以這樣做:

if($_SERVER[REQUEST_URI] == "/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')"; 
} 
else{ 
    $sql = "SELECT SUM(InvoiceAmount) AS TotalBilling,SUM(ClientCostToDate) AS WIP FROM Estimates WHERE date_format(InvoiceDate, '%Y-%m')=date_format(now(), '%Y-%m')"; 
} 
+0

這將工作,但它仍然需要他有多個文件(每個城市一個)。爲了DRY(不要重複自己),gview的答案更好。 –

+0

@BrianSchroeter我完全同意。如果OP只是在尋找這樣的事情,我只是給出了「明顯的直接解決方案」。但gview的答案是正確的。 – nanocv

3

是的,這是您可以使用服務器端腳本完成的最基本類型的事情。

首先你不需要多個腳本。 index.php將會很好。

添加studio的參數。

所以可選擇你會使用兩種:

http://../index.php 

http://..index.php?studio=london 

應該在這一點上,這樣做的好處是顯而易見的,當你有其他電影比其他數據庫倫敦,這個代碼仍然有效。

在腳本的頂部:

if (isset($_GET['studio']) { 
    $studio = $_GET['studio']; 
} else { 
    $studio = ''; 
} 

現在,您可以根據運行不同的查詢是否有傳遞的URL參數。

if ($studio == '') { 
    // $sql = ... 
} else { 
    $sql = "SELECT SUM(InvoiceAmount) AS TotalBilling,SUM(ClientCostToDate) AS WIP FROM Estimates WHERE Studio = '$studio' date_format(InvoiceDate, '%Y-%m')=date_format(now(), '%Y-%m')"; 
} 

所以,敷衍的評論 - 這段代碼很容易被sql注入。你如何做查詢的細節取決於你使用的客戶端庫。您應該使用mysqli或PDO和參數綁定,而不是將變量放入字符串中,然後綁定該變量。

例如:

$sql = "SELECT SUM(InvoiceAmount) AS TotalBilling,SUM(ClientCostToDate) AS WIP FROM Estimates WHERE Studio = :studio date_format(InvoiceDate, '%Y-%m')=date_format(now(), '%Y-%m')"; 

看看對庫MySQLi或PDO更多細節相應的手冊條目。