[英]mysql php dynamic sql query
Edit: Editing my question as the question has changed since originally posted.编辑:编辑我的问题,因为问题自最初发布以来发生了变化。
As per Rajesh's suggesstion.根据拉杰什的建议。 I have now modified the php code.我现在已经修改了 php 代码。
Here is the new code, but it still does not work.这是新代码,但它仍然不起作用。
Basically I want to run the same query several times but each time it will be group by different column.基本上我想多次运行相同的查询,但每次都会按不同的列进行分组。
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->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>';
}
?>
This code still does not work.此代码仍然不起作用。 It gives me a blank screen.它给了我一个空白屏幕。
However if I run this query on mysql it returns the data, there is no problem with the query.但是,如果我在 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>
Your help is appreciated.感谢您的帮助。 Thanks well in advance.提前致谢。
Define $objDatabase
like in the following像下面这样定义$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();
}
?>
use the query string below使用下面的查询字符串
$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."'";
And you can get the $typeQuery
like in the below你可以像下面这样得到$typeQuery
$type="capacity";
$typeQuery = $basequery." GROUP BY ".$type;
give a try试一下
Use foreach
to fetch the result instead of while
使用foreach
代替while
获取结果
$objDbResultByType = $objDatabase->query($typeQuery);
foreach ($objDbResultByType as $row) {
/*your code here*/
}
let me know if it is helpful让我知道它是否有帮助
You need quotes around the dates.您需要在日期周围引用。 And you should be using BETWEEN
, not >=
, to test if the date is between the start and end dates.并且您应该使用BETWEEN
而不是>=
来测试日期是否在开始日期和结束日期之间。
You also need a space after AS total
;在AS total
之后还需要一个空格; otherwise, the query ends up being COUNT(*) AS totalFROM hdds
, which is causing your syntax error.否则,查询最终是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' ";
There's really no need to end the string on each line.真的没有必要在每一行结束字符串。 PHP allows you to have line breaks in strings, so you can just continue the string across all the lines: 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.