// I wrote:
SELECT * FROM `wallposts` WHERE '%searchterm%' IN( `content`,`username` )
// you wrote:
SELECT * FROM 'wallposts' WHERE '%$searchq%' IN('content','username')
These are different. Identifiers (e.g., table/column names) are delimited with backticks ( ` ); strings are delimited with apostrophes ( ' ).
Backticks aren’t always necessary (you can safely leave them off in this case), but IMO they’re a good habit to get into. They can prevent problems where a table/column name conflicts with a reserved word:
SELECT key FROM table WHERE values = 'whatever'
# causes an SQL error, since KEY, TABLE, and VALUES are all reserved words
SELECT `key` FROM `table` WHERE `value` = 'whatever'
# works fine
Obviously, the best solution to this is to not use reserved words as your table/column names. However, there are quite a lot of them, some are obscure, new ones are added from time to time, and sometimes the word is such a “natural choice” for your data structure that you completely overlook the conflict — so I feel it’s best to be explicit about it.
(On most US keyboards, the backtick key is next to the 1 key, top left.)
[ edit ] p.s.:
If this is a new project, you might consider using mysqli (or PDO) instead of the mysql extension. ext_mysql is outdated (since MySQL 5) and is scheduled to be deprecated.
Viewing 4 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic.
*May or may not contain any actual "CSS" or "Tricks".