Eureka
Eureka

Reputation: 3

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!

Upvotes: 0

Views: 223

Answers (3)

Rowland Shaw
Rowland Shaw

Reputation: 38130

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

Upvotes: 0

oneat
oneat

Reputation: 10994

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

Upvotes: -1

BoltClock
BoltClock

Reputation: 723438

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!

Upvotes: 4

Related Questions