0

i'm trying to use the statement like of sql but unfortunately I get an error every time I use the query. In a php file I have:

$b = "SELECT Nombre, Link, Img_Pre, Precio, Descripcion, ID, Categoria, Subcategoria 
    FROM Productos WHERE Nombre LIKE \"%$a%\"";

but i get this error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\"%THE VALUE OF A%\"' at line 1

If i try the same statement on phpmyadmin i don't have any error. Why with php I get this error?

Thanks!

4
  • Have you tried using phpmyadmin's 'create PHP code' feature? Sometimes I find that can help with little syntax errors you might otherwise not find. Just copy/paste the code it created in place of your quoted code. Commented Sep 4, 2010 at 11:31
  • That's some mistype or whatever of the kind. there shouldn't be any errors, unless you're doing something unusual. there shouldn't be any \ symbols in the query. Something adding it, Doublecheck yourself. It's not PHP not mysql error, but just your mistake Commented Sep 4, 2010 at 11:32
  • what is the code between this line and query execution? Commented Sep 4, 2010 at 11:45
  • Can you provide us the $a value, try "LIKE \"%\$a\%\""; Commented Sep 4, 2010 at 12:06

3 Answers 3

4

Use single quotes in your LIKE string instead (I believe phpMyAdmin magically converts that to single quotes for you before running the query):

$b = "SELECT Nombre, Link, Img_Pre, Precio, Descripcion, ID, Categoria, Subcategoria FROM Productos WHERE Nombre LIKE '%$a%'";

Make sure you've escaped $a using mysql_real_escape_string() too, because it might be breaking your queries by introducing unescaped quotes!

Sign up to request clarification or add additional context in comments.

4 Comments

double quotes under php are used for Literal chars, so "hey $username" would convert username into its value, but 'hey $username' would not.. it would literally print out $username. I believe that this is the same with mysql, there a separation of string containers to make it easy for the user to specify what to be parsed, such as escape chars etc.
@RobertPitt you are wrong. there is nothing nearly similar in mysql. Both quotes are equal in every way.
@Col. Shrapnel: I don't know, PHP's MySQL extension just acts that way.
BoltClock nope, MySQL extension acts ether way. should of consulted the docs first as that dude just said.
0

Try:

$b = "SELECT Nombre, Link, Img_Pre, Precio, Descripcion, ID, Categoria, Subcategoria FROM Productos WHERE Nombre LIKE '%$a%'";

Some SQL queries will croak if you don't use apostrophes as the string delimeter

2 Comments

What queries? Got an example?
@Col Depends entirely on the whether you've got ANSI_QUOTES set: dev.mysql.com/doc/refman/4.1/en/…
-1
$b = "SELECT Nombre, Link, Img_Pre, Precio, Descripcion, ID, Categoria, Subcategoria 
    FROM Productos WHERE Nombre LIKE \"%"+$a+"%\"";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.