douqianbiao4216 2015-09-04 11:01
浏览 40
已采纳

mysql php动态组由sql查询

I am trying to get the php mysql query to return the result dynamically group by field.

For some reasons it is not working here is the code that I am using.

<?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>';
}


    ?>

The code above gives me blank screen.

However if I run this query on mysql it returns the data, there is no problem with the query.

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>

Can someone please help what I am doing wrong and why this is not working?

Thanks well in advance for your help.

  • 写回答

1条回答 默认 最新

  • 普通网友 2015-09-04 11:35
    关注

    Managed to fix it by using following.

    <?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>';
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址