july13 july13 - 6 months ago 12
MySQL Question

SQL statement not working SELECT 1064 Error in your SQL syntax

My SQL statement doesn't work...

Here is my variable:

$email="test@test.com";


These statements doesn't work :

$sql = "SELECT * FROM table WHERE email = $email";
$sql = 'SELECT * FROM table WHERE email = ' . $email;


1064 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 '@test.com' at line 1

But if I use a string instead of the variable, it works:

$sql = "SELECT * FROM table WHERE email = 'test@test.com'";


What's wrong with my statement?
Thanks!

Answer

please escape strings in mysql

$sql = "SELECT * FROM table WHERE email = $email"; 
$sql = 'SELECT * FROM table WHERE email = ' . $email;

This should work

$sql = "SELECT * FROM table WHERE email = '$email'";