General Community > Scripting Help

[MySQL]What's wrong with this ?

(1/2) > >>

Saleh:

--- Code: ---SELECT title, bookID, authF, authL, price
          WHERE title LIKE '%$title2%'
          ORDER BY price;
--- End code ---
what is wrong with this damn MySQL ?
it gives me this :
You have an error in your SQL syntax near 'ORDER BY price WHERE title LIKE '%enhanced%'' at line 2

۞:
Did you put a number by the wildcards? I believe % only works with text.

Haase:
He did put a number by the wildcard, %title2%.

I'm not sure about mySQL, but in Oracle, you can do this.  At work I maintain an order system and I'm always using "WHERE order_number LIKE '1302%';"

You should try troubleshooting by first taking out the ORDER BY clause, and see if that resolves your error. If you still have an error, put ORDER BY back in and get rid of your WHERE clause.

I have a sneaky suspicion that it doesn't like the fact that you've put the variable between wildcards.  Maybe try like '%{$title2}%' instead.

Or probably even better, right before the query, put this line:
--- Code: ---$title2 = '%' . $title2 . '%';
--- End code ---
and then make your query look like this:
--- Code: ---SELECT title, bookID, authF, authL, price
         WHERE title LIKE '$title2'
         ORDER BY price;
--- End code ---

Saleh:
ok I am sorry I just rushed in posting ,,,
the problem was I didn't specify what table to SELECT from it !! :P
and that wildcard thing .. title2 is a varibal handled via POST form ..  :D
after I specified the table it went okay ;)
it should be like this :

--- Code: ---SELECT title, bookID, authF, authL, price
FROM books
WHERE title LIKE '%$title2%'
ORDER BY price;
--- End code ---
FROM books was missing :P

and about wildcard handeling numbers ,, it's possible .. no problem in that ..
I am just new in PHP and MySQL but I am learning pretty fast .. PRETTY FAST ;D

Zef Hemel:
I hope you applied mysql_escape_string() to that $title2 POST field ;)

Navigation

[0] Message Index

[#] Next page

Go to full version