LDMartin1959 Posted December 19, 2013 Share Posted December 19, 2013 Working on a WordPress site. I've been again thrown into the deep end of the pool on this. I am trying to add some code to the HEADER.PHP file which will display content under certian conditions. Here's the code in question: $q1=mysql_query("SELECT * FROM 'wp_sean_sponsors' WHERE 'type' LIKE 1 AND 'cat'='.$post->ID.' "); $row_diamond=mysql_fetch_object($q1); if ($q1){ ?> The problem is that there doesn't seem to be anything coming back from the database. When I do a var_dump of $q1 it comes back as boolen(false), which I am taking to mean it has no value. But I know that a record which matches the criteria exists. I tried replacing '.$post->ID.' with a hard value with no change. I've got no clue where to go next but they're beating me until I come up with a solution. Quote Link to comment https://forums.phpfreaks.com/topic/284864-query-fails-newbie-needing-help-asap/ Share on other sites More sharing options...
Solution Barand Posted December 19, 2013 Solution Share Posted December 19, 2013 Don't put column names in quotes - it treats them as string values if you do that Quote Link to comment https://forums.phpfreaks.com/topic/284864-query-fails-newbie-needing-help-asap/#findComment-1462796 Share on other sites More sharing options...
LDMartin1959 Posted December 20, 2013 Author Share Posted December 20, 2013 You'd think when you use the MySQL query function to create the query it'd be smart enough not to do that!! Thanks. That solves that problem. Quote Link to comment https://forums.phpfreaks.com/topic/284864-query-fails-newbie-needing-help-asap/#findComment-1462799 Share on other sites More sharing options...
Barand Posted December 20, 2013 Share Posted December 20, 2013 Consider CREATE TABLE search_example (category INT PRIMARY KEY,text VARCHAR(20)); INSERT INTO search_example VALUES (1, 'category'),(2, 'description'),(3, 'text'); This next query searches for column category = column text (as expected none found) mysql> SELECT category FROM search_example WHERE text = category; Empty set (0.00 sec) Now we put category string in quotes to tell SQL "this is a string value" mysql> SELECT category FROM search_example WHERE text = 'category'; +----------+ | category | +----------+ | 1 | +----------+ 1 row in set (0.00 sec) SQL can't know what you intended. It's up to you to be smart enough. Quote Link to comment https://forums.phpfreaks.com/topic/284864-query-fails-newbie-needing-help-asap/#findComment-1462845 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.