0

I have the following code but my mysql query is not returning me any value. Please tell me what am I doing wrong:

foreach($stagearray as $catgry){
    echo $catgry;//this is returning the values
    $sql = mysql_query("
        select
            count(reference) as CCOUNTS,
            assignee_group
        from
            issue_tracking_issues i,
            issue_tracking_issues_issue_tracking_projects j
        where
            status NOT LIKE 'Closed%'
            AND i.id=j.issue_tracking_issue_id
            AND j.issue_tracking_project_id=1
            and $value
        having
            SUBSTR(date_raised,3,3)
        group by
            assignee_group
    ");

    while($rows = mysql_fetch_array($sql)){

This is giving me the following warning:

PHP Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

I am guessing that my sql query is not able to resolve $catgry but I don't know why

Travesty3
  • 14,608
  • 6
  • 56
  • 96
Bhaskar
  • 341
  • 5
  • 10

1 Answers1

1

You don't check the return value of mysql_query(). When mysql_query() fails for any reason, it returns false. When you put this false value into mysql_fetch_array(), it complains of course, because it expects a query result set and not just a false value.

Add at least or die(mysql_error()); in order to see, what's wrong with your query

$sql = mysql_query("select count(reference) as CCOUNTS,assignee_group from issue_tracking_issues...") or die(mysql_error());

Looking at the query, I guess the culprit could be the having in the where clause. having cannot be used this way as an operator, because it selects from the group by set. This should be

select ... from ... where ... group by ... having ...

If you want to compare $value against a substring, use a comparison operator like = or like or regexp.

See SELECT Syntax for details.

OT: mysql* is deprecated by now. Consider switching to either mysqli* or PDO.

Olaf Dietsche
  • 69,448
  • 7
  • 95
  • 188