Forum Moderators: coopster

Message Too Old, No Replies

MySQL Not Displaying Properly

PHP gives different results than MySQL Terminal

         

teamcoltra

4:37 pm on Aug 13, 2011 (gmt 0)

10+ Year Member



I am having a weird issue where PHP is giving me a different result than my MySQL terminal.

$date1 = date('Y-m-d')." 00:00:00";
$date2 = date('Y-m-d')." 24:00:00";

$hour1 = "select count(*) from sales where datetime between '".date('Y-m-d')." 07:55:00' and '".date('Y-m-d')." 09:00:00' AND misc2='DNU';";
$hour2 = "select count(*) from sales where datetime between '".date('Y-m-d')." 09:00:01' and '".date('Y-m-d')." 10:00:00' AND misc2='DNU';";
$hour3 = "select count(*) from sales where datetime between '".date('Y-m-d')." 10:00:01' and '".date('Y-m-d')." 11:00:00' AND misc2='DNU';";
$hour4 = "select count(*) from sales where datetime between '".date('Y-m-d')." 11:00:01' and '".date('Y-m-d')." 12:00:00' AND misc2='DNU';";
$hour5 = "select count(*) from sales where datetime between '".date('Y-m-d')." 12:00:01' and '".date('Y-m-d')." 13:00:00' AND misc2='DNU';";
$hour6 = "select count(*) from sales where datetime between '".date('Y-m-d')." 13:00:01' and '".date('Y-m-d')." 14:00:00' AND misc2='DNU';";
$hour7 = "select count(*) from sales where datetime between '".date('Y-m-d')." 14:00:01' and '".date('Y-m-d')." 15:00:00' AND misc2='DNU';";
$hour8 = "select count(*) from sales where datetime between '".date('Y-m-d')." 15:00:01' and '".date('Y-m-d')." 16:00:00' AND misc2='DNU';";
$hour9 = "select count(*) from sales where datetime between '".date('Y-m-d')." 16:00:01' and '".date('Y-m-d')." 17:00:00' AND misc2='DNU';";
$hour10 = "select count(*) from sales where datetime between '".date('Y-m-d')." 17:00:01' and '".date('Y-m-d')." 18:00:00' AND misc2='DNU';";
$hour11 = "select count(*) from sales where datetime between '".date('Y-m-d')." 18:00:01' and '".date('Y-m-d')." 19:00:00' AND misc2='DNU';";
$hour12 = "select count(*) from sales where datetime between '".date('Y-m-d')." 19:00:01' and '".date('Y-m-d')." 21:00:00' AND misc2='DNU';";
$test = "select count(*) from sales where datetime between '".date('Y-m-d')." 19:00:01' and '".date('Y-m-d')." 21:00:00' AND misc2='DNU';";


$hour1 = mysql_fetch_row(mysql_query($hour1));
$hour2 = mysql_fetch_row(mysql_query($hour2));
$hour3 = mysql_fetch_row(mysql_query($hour3));
$hour4 = mysql_fetch_row(mysql_query($hour4));
$hour5 = mysql_fetch_row(mysql_query($hour5));
$hour6 = mysql_fetch_row(mysql_query($hour6));
$hour7 = mysql_fetch_row(mysql_query($hour7));
$hour8 = mysql_fetch_row(mysql_query($hour8));
$hour9 = mysql_fetch_row(mysql_query($hour9));
$hour10 = mysql_fetch_row(mysql_query($hour10));
$hour11 = mysql_fetch_row(mysql_query($hour11));
$hour12 = mysql_fetch_row(mysql_query($hour12));
$test1 = mysql_fetch_row(mysql_query($test));

echo $test;
echo "<br>";
echo $test1['0'];
echo "<br>";



Now if I put in 1 sale in hour one and 3 in hour two and 2 in hour three the result should be:
1 3 2
when I echo $hour1['0']; echo $hour2['0']; echo $hour3['0'];

However, the result is:
1 4 6

when I put the generated mysql into a mysql terminal it generates correctly. Here is the output of above:

select count(*) from sales where datetime between '2011-08-13 19:00:01' and '2011-08-13 21:00:00' AND misc2='DNU';
10


Now here is the output of MySQL:

SELECT COUNT(*) FROM sales WHERE datetime between '2011-08-13 19:00:01' AND '2011-08-13 21:00:00' AND misc2='DNU'
Found: 1. Time: 0.069 sec. / Refetch / Export to CSV: pipe - tab - comma - semicolon

count(*)
0


Please note that the time is in the future so 0 is the correct response.

Also notice that they are running the exact same command.

Whats going on?

penders

5:19 pm on Aug 14, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I don't think datetime is a reserved word, but you could try quoting this (with backtics) just in case?

...1 sale in hour one and 3 in hour two and 2 in hour three the result should be:
1 3 2

However, the result is:
1 4 6


Is it just a coincidence that 4 is the sum of hours one+two and 6 is the sum of hours one+two+three?

coopster

12:43 pm on Aug 18, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Also check you php configuration.
[php.net...]