[RESOLVED] Trouble executing query and displaying results
I'm pretty new to PHP/MySQL. Assuming I've connected to the database, which I can do, I'm just wanting to retrieve rows from the db that meet the "WHERE" criteria in the code here, and then display them in the table code shown as long as results exist.
What am I doing wrong?
PHP Code:
$result = mysql_query("SELECT DATE_FORMAT(date,'%b %d') as audio_date, `title`, `intro`, `link` FROM `tbl_cssAudio` WHERE `interview` = `yes` ORDER BY event_date");
You are using back ticks around the value you are checking for (yes) - you should be using single quotes. You may also find that date is a reserved word and needs to be enclosed in back ticks, you can check that in the mysql manual.
Incidentally using the mysql_error() function will pass back the error from mysql itself which is useful for debugging and when asking for help.
Just as a general tip, it's usually best to store booleans as some form of int datatype rather than 'yes' or 'no' strings. It makes condition testing much easier since in PHP 1 = true and 0 = false, but 'yes' and 'no' both = true, which means you have to write less straightforward code if you ever come to test the values in such a field. Not applicable in this case, but just a general point.
$result = mysql_query("SELECT DATE_FORMAT(date,'%b %d') as audio_date, `title`, `intro`, `link` FROM `tbl_cssAudio` WHERE `interview` = `yes` ORDER BY event_date");
Should be
PHP Code:
$result = mysql_query("SELECT DATE_FORMAT(date,'%b %d') as audio_date, `title`, `intro`, `link` FROM `tbl_cssAudio` WHERE `interview` = 'yes' ORDER BY event_date");
`yes` isn't a string, it's looking for a mysql column\variable named yes. Also, those errors you received usually mean the SQL wasn't executed.
Thanks guys. In addition to the back ticks, I figured out that I was using a field/value in the WHERE clause that hadn't been retrieved from the database in the SELECT clause.
Bookmarks