Erik Soderstrom
Erik Soderstrom

Reputation: 313

MySQL query runs fine in command line, but gives error when ran in php

I have a query that works fine when I run it on my linux server command line, but when i try to execute i through php, it throws a php error.

SELECT h.timestamp, d.valueparam, r.label, r.dataUnit 
FROM EventData d, EventHeader h, EventKeyReference r 
WHERE h.id = d.eventHeader_id AND h.serialNumber=$serialnumber 
AND d.keyParam='G002' AND r.EventKey = 'G002' 
ORDER BY h.timestamp DESC LIMIT 1;

It gives me 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 'AND d.keyParam='G002' AND r.EventKey = 'G002' ORDER BY h.timestamp DESC LIMIT 1' at line 1

Does anyone know what's wrong?

Upvotes: 0

Views: 182

Answers (3)

nl-x
nl-x

Reputation: 11832

Change

AND h.serialNumber=$serialnumber 

into

AND h.serialNumber='$serialnumber' 

This will catch any empty $serialnumber or $serialnumber with letters in it.

Upvotes: 0

Praveen D
Praveen D

Reputation: 2377

Update your query as below, it will work even serialnumber is empty

$sql = "SELECT h.timestamp, d.valueparam, r.label, r.dataUnit 
FROM EventData d, EventHeader h, EventKeyReference r 
WHERE h.id = d.eventHeader_id AND h.serialNumber='".$serialnumber."'
AND d.keyParam='G002' AND r.EventKey = 'G002' 
ORDER BY h.timestamp DESC LIMIT 1";

Upvotes: 0

Halcyon
Halcyon

Reputation: 57729

My guess: $serialnumber is empty, your query is:

SELECT h.timestamp, d.valueparam, r.label, r.dataUnit 
FROM EventData d, EventHeader h, EventKeyReference r 
WHERE h.id = d.eventHeader_id AND h.serialNumber=
                                               // ^-- error here
AND d.keyParam='G002' AND r.EventKey = 'G002' 
ORDER BY h.timestamp DESC LIMIT 1;

Upvotes: 4

Related Questions