PHP does not return an error because it knows nothing about valid sql. It's just knows if it's a valid PHP statement. (which it is because you've got the "'s in the right place and a ; at the end. ) :)
It's up to MySQL to return an error.
As to your statement. 1: It's easier and valid in PHP to write it: $sqlwrk = "SELECT pk_phone_reports, SUM(calls) AS total_calls, date, calls , fk_ph_num FROM phone_reports WHERE (pk_phone_number = {fk_phone}) AND (date BETWEEN '{$my_startdate}' AND '$my_enddate')â GROUP BY pk_phone_reports, fk_ph_num, date, calls";
In reformatting the string I found a couple of PHP things that are probably what's tripping you up. PHP used the "." as a string concatonator. You were missing several of them. Notice that I remove them all. Because we enclosed the entire statement in a sing " pair, we can use {$variable} for substitution. Makes life a lot easier when building sql statements.
Also, I removed all the back-tiks. Not because they were wrong but they annoy my and in 99% of the cases are not necessary.
Oh and welcome to PHP/MySQL. I hope you'll find the language easy and the people friendly.
Finally, a could of things that will make life easier for you if you are going to be doing much database work in PHP/MySQL.
http://php.weblogs.com/
This is the best database abstraction layer I've found. Even if you only use MySQL, it's worth the investment in time to learn it. (mainly for the debug feature.)
www.sqlyog.com
Best FE for MySQL on Windows I've ever found. It's $49.00 (I think...I forget) but it's worth it. It's got it's bugs but overall it's a killer tool.
http://www.fabforce.net/dbdesigner4/
A killer, open source tool for designing databases. If you are used to the commercial tools costing $4k+ then you'll feel right at home with this. (It's my understanding that MySQL has purchased this project, but I may have my story wrong.)
Finally, you've already found the greatest tool for debugging MySQL/php, the lists.
See ya round. =C=
: : Cal Evans : Evans Internet Construction Company : 615-360-3385 : http://www.eicc.com : Building web sites that build your business :
Thompson, Jimi wrote:
For some background, I've been tasked with building a marketing tracking application by the PHB's who think that being able to write SQL means you can code. I know how to get data into a database and I can do thing with it once it's in there, but this is one of my first attempts at extracting anything remotely end-user-ish. Since the only server I can get is an old cobalt RAQ 2, the only database I can run is MySQL. Iâm not terribly familiar with MySQL (spent more time working with commercial databases) and Iâm a complete newbie at PHP, so please donât flame me yet â
Iâm not even sure what information Iâll need to provide you so here goes: Platform â Red Hat 9.0 Linux on a BogoMIPS CPU PHP Version - 4.3.3 Apache Version - 1.3.28 MySQL Version - 4.0.14
Hereâs my SQL statement which works fine from a DBA perspective (meaning that I can execute it from the command line against the database and obtain the desired results), but Iâm obviously missing something in the syntax in converting this to an acceptable PHP SQL statement.
I know that I can connect to the database and can extract other records, but I keep getting âunable to parseâ error message and donât know enough to know which thing Iâm doing is wrong.
SELECT phone_reports.pk_phone_reports,
SUM(phone_reports.calls) AS total_calls,
phone_reports.fk_ph_num,
phone_reports.`date`,
phone_reports.calls
FROM
phone_reports
WHERE
(phone_reports.fk_ph_num = 1) AND (phone_reports.`date` BETWEEN '2004/05/17' AND '2004/07/05')
GROUP BY
phone_reports.pk_phone_reports,
phone_reports.fk_ph_num,
phone_reports.`date`,
phone_reports.calls
Hereâs the PHP SQL statement built from the SQL statement above â
<?php if ($fk_phone != NULL) { $sqlwrk = "SELECT `pk_phone_reports`, `date`, `calls` , `fk_ph_num` FROM `phone_reports`"; $sqlwrk .= " WHERE `pk_phone_number` = " . $fk_phone; $rswrk = mysql_query($sqlwrk); if ($rswrk && $rowwrk = mysql_fetch_array($rswrk)) { echo $rowwrk["number"]; } @mysql_free_result($rswrk); } ?>
This seems to work ok, but doesnât return any results (which I expected) but it does parse! So then I try do this â
<?php if ($fk_phone != NULL) { $sqlwrk = "SELECT `pk_phone_reports`, SUM(`calls`) AS `total_calls`, `date`, `calls` , `fk_ph_num` FROM `phone_reports`"; $sqlwrk .= " WHERE `pk_phone_number` = " . $fk_phoneâ; $rswrk = mysql_query($sqlwrk); if ($rswrk && $rowwrk = mysql_fetch_array($rswrk)) { echo $rowwrk["number"]; } @mysql_free_result($rswrk); } ?>
Note that this shouldnât work since it isnât a valid SQL statement. I'm not sure why PHP doesn't return some kind of an error message. I know that the database does! You cannot execute SUM without its required âGROUP BYâ statement (at least against the database directly) but it at least parses as PHP. So then I expand by statement to include the rest of the âWHEREâ clause.
<?php if ($fk_phone != NULL) { $sqlwrk = "SELECT `pk_phone_reports`, SUM(`calls`) AS `total_calls`,`date`, `calls` , `fk_ph_num` FROM `phone_reports`"; $sqlwrk .= " WHERE (`pk_phone_number` = " . $fk_phoneâ) AND (`date` BETWEEN â'$my_startdate'â AND â'$my_enddate'â)â; $sqlwrk .= " GROUP BY `pk_phone_reports`, `fk_ph_num`, `date`, `calls`; $rswrk = mysql_query($sqlwrk); if ($rswrk && $rowwrk = mysql_fetch_array($rswrk)) { echo $rowwrk["number"]; } @mysql_free_result($rswrk); } ?>
Which brings me to my lovely parse error "Parse error: parse error in ad_report.php on line 138." What I want this to do is to return to me the "SUM" of all the calls to a specific phone number between two dates (the date the advertisement started running and the date it ended) and this display this in a cell in table on a web page.
Any help or pointers greatly appreciated.
Thanks,
Ms. Jimi Thompson, CISSP Manager, Web Operations Cox School of Business Southern Methodist University
"If we want women to do the same work as men, we must teach them the same things." - Plato
-- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php