doukefu1361 2013-04-17 20:08
浏览 41
已采纳

php中的mysql查询不是读取php变量[重复]

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

</div>
  • 写回答

1条回答 默认 最新

  • douzheng0702 2013-04-17 20:13
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?