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.

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

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀