Throwing Error..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
noctorum
Forum Commoner
Posts: 31
Joined: Fri Jun 13, 2008 10:46 am

Throwing Error..

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Throwing Error..

Post by Christopher »

You might want to try: WHERE Date='{$row['Date']}'");

What is the value of $row['Date']?
(#10850)
noctorum
Forum Commoner
Posts: 31
Joined: Fri Jun 13, 2008 10:46 am

Re: Throwing Error..

Post by noctorum »

Works, thanks.

How can I get that to echo?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Re: Throwing Error..

Post by Ambush Commander »

mysql_fetch_array()
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Throwing Error..

Post 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.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Re: Throwing Error..

Post 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.)
noctorum
Forum Commoner
Posts: 31
Joined: Fri Jun 13, 2008 10:46 am

Re: Throwing Error..

Post 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?];
        }
        
        
    }
 
noctorum
Forum Commoner
Posts: 31
Joined: Fri Jun 13, 2008 10:46 am

Re: Throwing Error..

Post 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.
noctorum
Forum Commoner
Posts: 31
Joined: Fri Jun 13, 2008 10:46 am

Re: Throwing Error..

Post 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
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Re: Throwing Error..

Post 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
noctorum
Forum Commoner
Posts: 31
Joined: Fri Jun 13, 2008 10:46 am

Re: Throwing Error..

Post 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.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Re: Throwing Error..

Post 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?
noctorum
Forum Commoner
Posts: 31
Joined: Fri Jun 13, 2008 10:46 am

Re: Throwing Error..

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Throwing Error..

Post by Christopher »

I think "date" is a reserved work. Maybe better would be: WHERE `Date`='{$row['Date']}'");
(#10850)
noctorum
Forum Commoner
Posts: 31
Joined: Fri Jun 13, 2008 10:46 am

Re: Throwing Error..

Post by noctorum »

Got it, working great. Thanks much.
Post Reply