user2350122 user2350122 - 23 days ago 6
MySQL Question

Why do I get a MySQL syntax error (1604) when I select by email address?

I compare email addresses in PHP from MySQL database:

$query
->select(array('a.dtstart','a.cal_id','b.name','a.email'))
->from('#__pbbooking_events AS a')
->join('INNER', '#__pbbooking_cals AS b ON (a.cal_id = b.id)')
->where('a.email = ' . $user->email)
->where(' a.dtstart>'. $query->currentTimestamp())
->order('a.dtstart ASC');


$user
just has information of the currently logged user.

I get this error


YOU HAVE AN ERROR IN YOUR SQL SYNTAX; CHECK THE MANUAL THAT
CORRESPONDS TO YOUR MARIADB SERVER VERSION FOR THE RIGHT SYNTAX TO USE
NEAR
@RET.RU AND A.DTSTART>CURRENT_TIMESTAMP() ORDER BY A.DTSTART
ASC
AT LINE 4


If I remove the
where
clause that compares emails, then the query runs without error.

I have read somewhere that the problem is the
@
sign but how can I make this comparison then?

Answer

$user->email is string so you need to use ' single brackets with email.

Try below

$query
    ->select(array('a.dtstart','a.cal_id','b.name','a.email'))
    ->from('#__pbbooking_events AS a')
    ->join('INNER', '#__pbbooking_cals AS b ON (a.cal_id = b.id)')
    ->where("a.email = '" . $user->email. "'") // $user->email is string
    ->where(' a.dtstart > '. $query->currentTimestamp())
    ->order('a.dtstart ASC');