1

I am using the mysqli db class found at github

my query looks like

$db->query('
  SELECT
    memberID,zoneCode,state,zone,countyName,
    CONCAT(state,\'Z\',zone) as fullZoneCode
  FROM members_zonesToWatch
  LEFT JOIN (
    SELECT state,zone,countyName
    FROM countyPublicForcastZoneCorrelation
  ) as zones
    ON zoneCode = CONCAT(state,\'Z\',zone)');

The issue im getting is

Fatal error: Problem preparing query 
(SELECT
  memberID,zoneCode,state,zone,countyName,
   CONCAT(state,'Z',zone) as fullZoneCode
 FROM members_zonesToWatch
 LEFT JOIN (
   SELECT state,zone,countyName
   FROM countyPublicForcastZoneCorrelation
 ) as zones
   ON zoneCode = CONCAT(state,'Z',zone)) 
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
''Z',zone) as fullZoneCode FROM members_zonesToWatch LEFT' at line 1

This query as it is works in mysql workbench just fine. What is wrong with it using the mysqli class?

2
  • Hi, I inserted some newlines to the query and the error message for readability. Commented Jul 7, 2012 at 5:03
  • Try to use " instead of \'. I know it's not different. But just try maybe works. Commented Jul 13, 2012 at 14:16

2 Answers 2

2
+50

After testing on your case, the problem is the 'Z' and $db->query

It is because the MysqliDB internally uses mysqli and mysqli throws the exception when prepare your statement (from the debugger, it seems the quote ' turns to ').

Instead, MysqliDB provide rawQuery for cases like yours. So use the below :

$result = $db->rawQuery('
    SELECT m.memberID, CONCAT(m.state, ? ,m.zone) as fullZoneCode
    FROM members_zonesToWatch m
    LEFT JOIN countyPublicForcastZoneCorrelation c
    ON c.zoneCode = CONCAT(m.state, ? ,m.zone)',
    array('Z','Z'));

Reference: PHP-MySQLi-Database-Class / MysqliDb.php;

Sign up to request clarification or add additional context in comments.

1 Comment

@bretterer the second parameter of rawQuery is simply an array of all used parameters
2

Shouldn't that be

SELECT
m.memberID, m.zoneCode, m.state, m.zone, m.countyName,
CONCAT(m.state,'Z',m.zone) as fullZoneCode,
c.state, c.zone, c.countyName
FROM members_zonesToWatch m
LEFT JOIN countyPublicForcastZoneCorrelation c
ON c.zoneCode = CONCAT(m.state, 'Z', m.zone) 

6 Comments

That gives me Fatal error: Problem preparing query (SELECT m.memberID, m.zoneCode, m.state, m.zone, m.countyName, CONCAT(m.state,'Z',m.zone) as fullZoneCode, c.state, c.zone, c.countyName FROM members_zonesToWatch m LEFT JOIN countyPublicForcastZoneCorrelation c ON c.zoneCode = CONCAT(m.state, 'Z', m.zone)) 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 ''Z',m.zone) as fullZoneCode, c.state, c.zone, c.countyName FROM members_' at line 3 and now does not work in mysql workbench. Says unknown col m.state
It seems then that you can't use CONCAT as one of the parameters of the ON statement
Is that a limitation of the class or mysqli? This works just fine with my old MySQL class.
@bretterer: "Unknown col m.state"? Is there a field 'state' in the table 'members_zonestowatch'? I would start anew with a query having one field and one table; add the necessary fields from that table until everything is ok, then add the second query in the same manner.
@Noam-newman: That was it... it should be just c.state, c.zone,c.countyName and the concat's should be c's as well... Thanks! HOWEVER, it still does not work within the mysqli class. I still get the same error
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.