Search code examples
phpmysqlmysql-error-1064

Problem with a mysql query (like) and php


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!


Solution

  • 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!