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 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测