Page 1 of 1

Throwing Error..

Posted: Mon Jun 16, 2008 3:53 pm
by noctorum

Code: Select all

 
$getDistinctDays = mysql_query("SELECT DISTINCT Date
FROM phonelog2
WHERE Date>='$start_date'
AND Date<='$end_date' ");
 
while ($row = mysql_fetch_array($getDistinctDays))
    {
        $countTotalPerDay = mysql_query("SELECT COUNT( * ) 
                                        FROM phonelog2
                                        WHERE Date=$row['Date'] ");
        
    }
Error:
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/public5/public_html/testing/alex_testing/phonequerynew.php on line 251
Any help appreciated.

edit: Line 251 corresponds to the second WHERE

Re: Throwing Error..

Posted: Mon Jun 16, 2008 4:12 pm
by Christopher
You might want to try: WHERE Date='{$row['Date']}'");

What is the value of $row['Date']?

Re: Throwing Error..

Posted: Mon Jun 16, 2008 4:19 pm
by noctorum
Works, thanks.

How can I get that to echo?

Re: Throwing Error..

Posted: Mon Jun 16, 2008 4:24 pm
by Ambush Commander
mysql_fetch_array()

Re: Throwing Error..

Posted: Mon Jun 16, 2008 4:29 pm
by Benjamin
You can combine the queries as well, to make it more efficient.

Code: Select all

 
      SELECT DISTINCT
        pl.`DATE`,
        (SELECT count(*) FROM phonelog2 pl2 WHERE pl2.`DATE` = pl.`DATE`) as Total
      FROM
        phonelog2 pl
      WHERE
        pl.`DATE`>='$start_date'
        AND pl.`DATE`<='$end_date' 
 
Actually, I believe you need backticks around the Date field as well.

Re: Throwing Error..

Posted: Mon Jun 16, 2008 4:31 pm
by Ambush Commander
If we're on the subject of code cleanup, make sure you've properly escaped $start_date/$end_date; better yet, use a DB parameter binding wrapper (AdoDB is a pre-canned one, but it shouldn't take more than five minutes to code your own.)

Re: Throwing Error..

Posted: Mon Jun 16, 2008 4:34 pm
by noctorum
Ambush Commander wrote:mysql_fetch_array()
Yep, I know. I'm just not sure how to call it, example;

Code: Select all

 
$getDistinctDays = mysql_query("SELECT DISTINCT Date
FROM phonelog2
WHERE Date>='$start_date'
AND Date<='$end_date' ");
 
while ($row = mysql_fetch_array($getDistinctDays))
    {
        $countTotalPerDay = mysql_query("SELECT COUNT( * ) 
                                        FROM phonelog2
                                        WHERE Date={$row['Date']}");
        
        while ($row = mysql_fetch_array($countTotalPerDay))
        {
            echo $row[what goes here?];
        }
        
        
    }
 

Re: Throwing Error..

Posted: Mon Jun 16, 2008 4:36 pm
by noctorum
Ambush Commander wrote:If we're on the subject of code cleanup, make sure you've properly escaped $start_date/$end_date; better yet, use a DB parameter binding wrapper (AdoDB is a pre-canned one, but it shouldn't take more than five minutes to code your own.)
Will do, next on my list. Just getting the skeleton running then I'll go through and clean it up.

Re: Throwing Error..

Posted: Mon Jun 16, 2008 4:51 pm
by noctorum
arborint wrote:You might want to try: WHERE Date='{$row['Date']}'");

What is the value of $row['Date']?
Standard date, format YYYY-MM-DD

Re: Throwing Error..

Posted: Mon Jun 16, 2008 4:52 pm
by Ambush Commander
Well, if you're not sure, you can always var_dump() the result and find out. I think, in this case, it's what goes here? = 0

Re: Throwing Error..

Posted: Mon Jun 16, 2008 4:55 pm
by noctorum
Ambush Commander wrote:Well, if you're not sure, you can always var_dump() the result and find out. I think, in this case, it's what goes here? = 0
I used print_r, but it didn't make sense as it showed 0 for the count. However, echoing $row[0] produces 0 as well.

Code: Select all

 
while ($row = mysql_fetch_array($getDistinctDays))
    {
        $countTotalPerDay = mysql_query("SELECT COUNT( * ) 
                                        FROM phonelog2
                                        WHERE Date={$row['Date']}");
        
        while ($row = mysql_fetch_array($countTotalPerDay))
        {
            echo $row[0];
        }
        
        
    }
Returns 0, although it should have been 5.

Re: Throwing Error..

Posted: Mon Jun 16, 2008 4:59 pm
by Ambush Commander
There might be something faulty with the query. What's the value of $row['Date'], and are you sure it's in your db?

Re: Throwing Error..

Posted: Mon Jun 16, 2008 5:08 pm
by noctorum
Ambush Commander wrote:There might be something faulty with the query. What's the value of $row['Date'], and are you sure it's in your db?
Yep.

Code: Select all

 
                        $getDistinctDays = mysql_query("SELECT DISTINCT Date
                        FROM phonelog2
                        WHERE Date>='$start_date'
                        AND Date<='$end_date' ");
 
                        while ($row = mysql_fetch_array($getDistinctDays))
                        {
                        echo $row['Date'];
                        echo "<br>";
                        }
Returns;
2007-04-23
2007-04-24
2007-04-25
2007-04-26
2007-04-27
2007-04-28
2007-04-29
2007-04-30
Where start date is 2007-04-23 and end date is 2007-04-30

Re: Throwing Error..

Posted: Mon Jun 16, 2008 5:22 pm
by Christopher
I think "date" is a reserved work. Maybe better would be: WHERE `Date`='{$row['Date']}'");

Re: Throwing Error..

Posted: Mon Jun 16, 2008 5:29 pm
by noctorum
Got it, working great. Thanks much.