tags:

views:

34

answers:

4

I have looked all over the internet for an answer to my question to why my sql statement returns false. I checked it out on the sql validator over at mimer and all I got was that I used the reserved word name. There should be something in my database that matches this so here it is:

Here is how I create the sql statement:

$title = 'SELECT * FROM item, categories WHERE item.title 
LIKE "%'.implode('%" OR item.title LIKE "%', $data).'%"'.' 
AND categories.name = '.$category;

And this is the result:

SELECT * FROM item, categories WHERE item.title LIKE "%hello%" 
OR item.title LIKE "%world%" OR item.title 
LIKE "%Joomla%" OR item.title LIKE "%Animal%" AND categories.name = Book
+3  A: 

You should probably think about your (lack of) parentheses. I'm not sure specifically what you are trying to accomplish,

but this:

SELECT
    *
FROM item, categories
WHERE item.title LIKE "%hello%"
OR item.title LIKE "%world%"
OR item.title LIKE "%Joomla%"
OR item.title LIKE "%Animal%"
AND categories.name = "Book"

is vastly different from this:

SELECT
    *
FROM item, categories
WHERE (
    item.title LIKE "%hello%"
    OR item.title LIKE "%world%"
    OR item.title LIKE "%Joomla%"
    OR item.title LIKE "%Animal%"
)
AND categories.name = "Book"

Also, like the others say, Book should be in quotes (like I have done here).

AND categories.name = "Book"
Gabriel McAdams
Excellent point. Also, you may want it to be: categories.name = 'Book'
sdcoder
This is still not going to work without quotes around Book.
Kaleb Brasee
Thanks, guys. I updated my answer.
Gabriel McAdams
A: 

You need to surround the word Book with quotes.

I'm betting you also need parentheses.

SELECT * FROM item, categories WHERE (item.title LIKE "%hello%" 
OR item.title LIKE "%world%" OR item.title 
LIKE "%Joomla%" OR item.title LIKE "%Animal%") AND categories.name = "Book"
Kaleb Brasee
A: 

the last part, categories.name, is that a string? if so it should be

and categories.name = 'book'

note the quotes around book

John Boker
also, use @gabriel's answer as you'll need parenthesis around the OR's
John Boker
A: 

Thank you guys so much. This has been extremely helpful. I am not the most knowledgeable when it comes to sql so sorry if this seemed like a stupid question.

snaderRaider