簡體   English   中英

mysql php動態sql查詢

[英]mysql php dynamic sql query

編輯:編輯我的問題,因為問題自最初發布以來發生了變化。

根據拉傑什的建議。 我現在已經修改了 php 代碼。

這是新代碼,但它仍然不起作用。

基本上我想多次運行相同的查詢,但每次都會按不同的列進行分組。

這是我正在使用的代碼。

<?php
    $servername = "localhost";
    $username = "user";
    $password = "pass";

    try {
        $objDatabase = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
        $objDatabase->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        echo "Connected successfully";
        }
    catch(PDOException $e)
        {
        echo "Connection failed: " . $e->getMessage();
        }
?>

<?php 

//Process the GET data received from the previous page.
$custo = $_GET["Customer"];
$startdate = $_GET["fromdate"];
$enddate = $_GET["enddate"];
$stdate = date("Y-m-d", strtotime($startdate));
$endate = date("Y-m-d", strtotime($enddate));


$basequery = "SELECT cust, manu, model, serial, capacity, firmware, method, date, stime, etime, wks, result, COUNT(*) AS total FROM hdds WHERE cust = '".$custo."' and `date` >= '".$stdate."' and `date` <= '".$endate."'";

$retval = mysql_query( $basequery, $conn );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}

?>


<? php 

$type="capacity";
$typeQuery = $basequery." GROUP BY ".$type;
// Perform the Query
$objDbResultByType = $objDatabase->Query($typeQuery);
echo '<div id="1000" style="display: none;">';
echo "<h3>Quality Control Checked by<br></h3><strong>";
$capacity = array();
while ($row = $objDbResultByType->FetchArray()) {
    echo $row['capacity']. " = " .$row['total'];
    echo "<br><strong>";
$result = "{ label: \"".$row['capacity']."\", y: " .$row['total']." },";
array_push($capacity,$result);
}
//echo $result;
$lastIndex = count($capacity)-1;
$lastValue = $capacity[$lastIndex];
$testedby[$lastIndex] = rtrim($lastValue, ',');

//Echo the capacity from array to the monitor screen. 
foreach ($capacity as $result){
echo $result, '<br>';
}

?>

此代碼仍然不起作用。 它給了我一個空白屏幕。

但是,如果我在 mysql 上運行此查詢,它會返回數據,則查詢沒有問題。

mysql> SELECT cust, manu, model, serial, capacity, firmware, method, date, stime, etime, wks, result, COUNT(*) AS total from hdds where cust = 'Imran-ABC' and date >= '2015-08-01' and '2015-09-14' group by date;
+-----------+------------------------------------------+------------+----------+-----------------------------+----------+--------+------------+----------+----------+------+-----------+-------+
| cust      | manu                                     | model      | serial   | capacity                    | firmware | method | date       | stime    | etime    | wks  | result    | total |
+-----------+------------------------------------------+------------+----------+-----------------------------+----------+--------+------------+----------+----------+------+-----------+-------+
| ABC | Seagate Barracuda 7200.7 and 7200.7 Plus | ST340014AS | 5MQ3DJPM | 40000000000 bytes [40.0 GB] | 8.12     | zero   | 2015-08-26 | 18:56:29 | 18:56:29 | 89   | Succeeded |     1 |
| ABC | Seagate Barracuda 7200.7 and 7200.7 Plus | ST340014AS | 5MQ3DJPM | 40000000000 bytes [40.0 GB] | 8.12     | zero   | 2015-09-01 | 18:56:29 | 18:56:29 | 89   | Succeeded |    27 |
| ABC | Seagate Barracuda 7200.7 and 7200.7 Plus | ST340014AS | 5MQ3DJPM | 40000000000 bytes [40.0 GB] | 8.12     | zero   | 2015-09-02 | 20:04:19 | 20:04:19 | 36   | Succeeded |     2 |
+-----------+------------------------------------------+------------+----------+-----------------------------+----------+--------+------------+----------+----------+------+-----------+-------+
3 rows in set, 1 warning (0.00 sec)

mysql>

感謝您的幫助。 提前致謝。

像下面這樣定義$objDatabase

<?php
    $servername = "localhost";
    $username = "your_username";
    $password = "your_password";

    try {
        $objDatabase = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
        $objDatabase->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        echo "Connected successfully"; 
        }
    catch(PDOException $e)
        {
        echo "Connection failed: " . $e->getMessage();
        }
    ?>

使用下面的查詢字符串

$basequery = "SELECT cust, manu, model, serial, capacity, firmware, method, date, stime, etime, wks, result, COUNT(*) AS total FROM hdds WHERE cust = '".$custo."' and `date` >= '".$stdate."' and `date` <= '".$endate."'";

你可以像下面這樣得到$typeQuery

$type="capacity";
$typeQuery = $basequery." GROUP BY ".$type;

試一下

使用foreach代替while獲取結果

$objDbResultByType = $objDatabase->query($typeQuery);

foreach ($objDbResultByType as $row) {
/*your code here*/
}

讓我知道它是否有幫助

您需要在日期周圍引用。 並且您應該使用BETWEEN而不是>=來測試日期是否在開始日期和結束日期之間。

AS total之后還需要一個空格; 否則,查詢最終是COUNT(*) AS totalFROM hdds ,這會導致您的語法錯誤。

$basequery = "SELECT cust, manu, model, serial, capacity, firmware, method, date, stime, etime, wks, result, COUNT(*) AS total ".
   "FROM hdds ".
   "WHERE cust = '$custo' and date BETWEEN '$stdate' AND '$endate' ";

真的沒有必要在每一行結束字符串。 PHP 允許您在字符串中使用換行符,因此您可以在所有行中繼續字符串:

$basequery = "SELECT cust, manu, model, serial, capacity, firmware, method, date, stime, etime, wks, result, COUNT(*) AS total
   FROM hdds
   WHERE cust = '$custo' and date BETWEEN '$stdate' AND '$endate' ";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM