2015-09-04 104 views
-2

我想讓php mysql查詢動態地按字段返回結果。由sql查詢mysql 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()) { 
    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> 

有人可以幫助我做錯了什麼,爲什麼這是行不通的?

非常感謝您的幫助。

+0

'<?php' not'<? php'。 –

+0

謝謝修復<?php但仍然是同樣的問題 – user2107349

+1

您正在將(不建議使用的)mysql_ *調用與PDO混合。另外,如果你得到一個空白屏幕檢查日誌,你的錯誤信息將在那裏。 –

回答

0

通過使用以下方法進行修復。

<?php 

$type="capacity"; 
$typeQuery = $basequery." GROUP BY ".$type; 
// Perform the Query 
$objDbResultByType = $objDatabase->Query($typeQuery); 
foreach ($objDbResultByType as $row) { 
preg_match('/\[(.*?)\]/', $row['capacity'], $matches); 
if (isset($matches[1])) { 
    $row['capacity'] = $matches[1]; 
} 
    echo $row['capacity']. " = " .$row['total']; 
    echo "<br><strong>"; 
$result = "{ label: \"".$row['capacity']."\", y: " .$row['total']." },"; 
array_push($capacity,$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>'; 
} 

?>