I have an error like this :

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\lab\try.php on line 40

The query is :

$graphquery = mysql_query ("SELECT
  $cluster,
  SUM(IF(idx=3,1,0)) AS green,
  SUM(IF(idx=2,1,0)) AS yellow,
  SUM(IF(idx=1,1,0)) AS red
FROM (SELECT
        $cluster
        $xcond
        ,LEAST(999 $acond) idx 
      FROM table
      WHERE date >= '". $startDate ."'
          AND date <= '". $endDate ."'
          AND $regional = '$gregcond'
          AND $latitude IS NOT NULL 
      GROUP BY $cluster, $lac, $mainid
      HAVING $cluster IS NOT NULL
$paraxcond
      ) A
GROUP BY $cluster");

If I echo the query, it will be like this (no error, perfect result too btw) :

SELECT
  cluster,
  SUM(IF(idx=3,1,0)) AS green,
  SUM(IF(idx=2,1,0)) AS yellow,
  SUM(IF(idx=1,1,0)) AS red
FROM (SELECT
        cluster,
        ROUND(AVG(some_value_Index))    some_value_Index,
        LEAST(999, AVG(some_value_Index) )    idx
      FROM TABLE
      WHERE tanggal >= '2012-07-10'
          AND tanggal <= '2012-08-02'
          AND Regional = 'Regional_Example'
          AND Latitude IS NOT NULL
      GROUP BY cluster, Lac, ID
      HAVING cluster IS NOT NULL
          AND some_value_Index IN(3,2,1)) A
GROUP BY cluster

when I add mysql_error on that script, it sounds like this :

Incorrect parameter count in the call to native function 'LEAST'

And here is how I willing to use the MYSQL Query :

$row = mysql_fetch_array ($graphquery)

When I am ECHO the query and try it on SQLYOG or PHPMYADMIN there is no error contained, so Can anyone tell me where is the error please? Thanks!

share|improve this question
whats the content of $acond? – Hawili Aug 2 '12 at 4:17
1  
you don't show us what is $graphquery and you use mysql_ which is even worse. Start using PDO or MySQLi. – alfasin Aug 2 '12 at 4:23
$acond and $xcond is just a condition bro, I think there is nothing that will affect the script. The query is too complex I think, so I cant explain more details. But I'll wait your answers or another question to clear it :) Thanks btw – Anggie Aziz Aug 2 '12 at 4:23
Try making your SQL Query a string prior to actually running the query ($qryStr = "SELECT...";). That way you can echo the $qryStr and see if one of the variables is causing an issue. Right now it's hard to tell because we don't know what's in $cluster, $xcond, etc. – Douglas A. Crosby Aug 2 '12 at 4:24
alfasin : I have update it, there is the $graphquery. Sorry – Anggie Aziz Aug 2 '12 at 4:25
show 4 more comments
feedback

1 Answer

You missed a comma in the LEAST() function. LEAST expects 2 or more parameters. So the query will be.

mysql_query ("SELECT
  $cluster,
  SUM(IF(idx=3,1,0)) AS green,
  SUM(IF(idx=2,1,0)) AS yellow,
  SUM(IF(idx=1,1,0)) AS red
FROM (SELECT
        $cluster
        $xcond
        ,LEAST(999, $acond) idx 
      FROM table
      WHERE date >= '". $startDate ."'
          AND date <= '". $endDate ."'
          AND $regional = '$gregcond'
          AND $latitude IS NOT NULL 
      GROUP BY $cluster, $lac, $mainid
      HAVING $cluster IS NOT NULL
$paraxcond
      ) A
GROUP BY $cluster");

I changed LEAST(999 $acond) to LEAST(999, $acond)

share|improve this answer
Thanks mate, but check the questions once more. It will be more clear to understand maybe :D – Anggie Aziz Aug 2 '12 at 4:30
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.