The following query works fine in phpMyAdmin:

SELECT xample.id,
  ( xample.email != '' ) , 
  ( xample.bild = '1' ) , 
  ( xample.url != '' ) , 
  COUNT ( DISTINCT xsub.id ) , 
  COUNT ( DISTINCT xsub2.id )
FROM xample 
LEFT JOIN xsub ON xample.id = xsub.xampleId 
LEFT JOIN xsub2 ON xample.id = xsub2.xampleId 
GROUP BY xample.id

I wonder why I got an MySQL-Syntax-Error from this PHP-Code:

$sql =<<<EOT
  SELECT xample.id,
    ( xample.email != '' ) , 
    ( xample.bild = '1' ) , 
    ( xample.url != '' ) , 
    COUNT ( DISTINCT xsub.id ) , 
    COUNT ( DISTINCT xsub2.id )
  FROM xample 
  LEFT JOIN xsub ON xample.id = xsub.xampleId 
  LEFT JOIN xsub2 ON xample.id = xsub2.xampleId 
  GROUP BY xample.id
EOT;
$res = mysql_query($sql,$lkId);

The queries are absolutely identical ... The same MySQL (3.xx) Server has been used....

Tnx 4 replies
McIheal

    you need to escape the single quotes in php or the statement will get funny looking ( trying echoing it out before you submit the query

    $sql =" SELECT xample.id,
    ( xample.email !=\'\' ) ,
    ( xample.bild = \'1\' ) ,
    ( xample.url != \'\') ,
    COUNT ( DISTINCT xsub.id ) ,
    COUNT ( DISTINCT xsub2.id )
    FROM xample

    LEFT JOIN xsub ON xample.id = xsub.xampleId

    LEFT JOIN xsub2 ON xample.id = xsub2.xampleId

    GROUP BY xample.id";

      tnx, but there was another problem i found accidentally by trying (escaping the quotes is not necessary)
      there are no whitespaces allowed in the COUNT(DISTINCT xx) statement.
      phpMyAdmin seems to delete the spaces before executing the query.

        Write a Reply...