Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

Edit: Editing my question as the question has changed since originally posted.

As per Rajesh's suggesstion. I have now modified the php code.

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> 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.

share|improve this question
    
Where is $objDatabase defined? – eggyal Sep 4 at 9:11
1  
@Rajeshkannan There's no need to post a comment about your own answer. – Barmar Sep 4 at 9:15
    
What is $baseQuery over here : $typeQuery = getQueryGroupBy($baseQuery, 'capacity'); – raveenanigam Sep 4 at 9:15
    
$objDatabase is not defined anywhere I guess, hence I am having problem. Can you help me fix this, – user2107349 Sep 4 at 9:31
    
If you don't know what $objDatabase is how should we? My guess is you just copied&pasted some other script fragments without understanding what they do? – VolkerK Sep 4 at 9:40

2 Answers 2

up vote 0 down vote accepted

Define $objDatabase like in the following

<?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

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

give a try

Use foreach to fetch the result instead of while

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

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

let me know if it is helpful

share|improve this answer
1  
Please explain what you changed and why. – Barmar Sep 4 at 9:14
1  
Actually I have done the following changes 1) added single quote before and after the php variable and done string concatination using (.) operator 2) Added backtick for date variable as it is reserve word (may be it is causing the issue) 3) and added date<=$enddate condition – Rajesh kannan Sep 4 at 9:18
1  
There is nothing wrong with this query, I am using this query in another page and it works fine, If you read the php part below, you will understand that I want to run this query dynamically group by " " the group by word will be unique, this is to avoid writing the entire query. I hope you understood – user2107349 Sep 4 at 9:20
1  
@Rajeshkannan Put that in the answer, not a comment. – Barmar Sep 4 at 9:22
1  
date is not a reserved word. Someone else posted that, he has already deleted his answer. – Barmar Sep 4 at 9:23

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.

You also need a space after AS total; otherwise, the query ends up being COUNT(*) AS totalFROM hdds, which is causing your syntax error.

$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:

$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' ";
share|improve this answer
    
It still won't work, because $objDatabase is undefined... – eggyal Sep 4 at 9:18
    
I'm going to ignore that and assume it's in code he didn't bother to include in the question. – Barmar Sep 4 at 9:22
    
Basically I copied the php part that contains $objDatabase hence it is not working. Could you help me write the php so that I can run this query "Group by" dynamically. – user2107349 Sep 4 at 9:34
    
You have a typo: getQueryGroupBy($baseQuery, 'capacity'); should be getQueryGroupBy($basequery, 'capacity');. The Q should be q. – Barmar Sep 4 at 9:36
1  
And just use mysql_query() instead of $objDatabase->query(). Why do you copy code without understanding what it's doing? That depends on using a database class that you're not loading. – Barmar Sep 4 at 9:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.