mysql-php动态分组sql查询
我试图让php mysql查询按字段动态返回结果 由于某些原因,我使用的代码在这里不起作用mysql-php动态分组sql查询,php,mysql,dynamic,Php,Mysql,Dynamic,我试图让php mysql查询按字段动态返回结果 由于某些原因,我使用的代码在这里不起作用 <?php $servername = "localhost"; $username = "user"; $password = "pass"; try { $objDatabase = new PDO("mysql:host=$servername;dbname=myDB", $username, $password); $objD
<?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>';
}
?>
有人能帮我一下我做错了什么,为什么这不起作用
提前非常感谢您的帮助。使用以下方法解决了此问题
<?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>';
}
?>
感谢修复了您将不推荐的mysql_*调用与PDO混合在一起的问题。另外,如果你得到一个空白屏幕,你的错误信息就会出现。在你的php中,你是按容量分组的,而在你的查询中,你使用的是日期。是故意的吗?坎皮,这是故意的
<?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>';
}
?>