dougou5844 2016-11-28 19:20
浏览 20
已采纳

如何计算查询结果的所有行出现次数

I am not sure if I am doing this wrong or not including something I should be, but I am using rowCount() to try and count all the rows in my database for the query results, like this: $count_total_friend = $friend_stmt->rowCount();. The results are showing 6 when it should be only 2. It seems as if it is counting all the rows in my database, rather than my queried results.

What am I doing wrong?

enter image description here

The

    $friend_status = 2;
    $friend_sql = "
        SELECT *
        FROM friends
        WHERE friend_one or friend_two = ?
        AND status = ?
    ";
    $friend_stmt = $con->prepare($friend_sql);
    $friend_stmt->execute(array($user_id, $friend_status));
    $friend_total_rows = $friend_stmt->fetchAll(PDO::FETCH_ASSOC);
    $count_total_friend = $friend_stmt->rowCount();
    foreach ($friend_total_rows as $friend_total_row) {
        //$select_friend_1  = $friend_total_row['friend_one'];
        //$select_friend_2  = $friend_total_row['friend_two'];
        //$friend_status        = $friend_total_row['status'];
        //$friend_status_date = $friend_total_row['date'];
    }
  • 写回答

1条回答 默认 最新

  • duanji2772 2016-11-28 19:22
    关注

    There is a problem with your query

        SELECT *
        FROM friends
        WHERE friend_one or friend_two = ?
        AND status = ?
    

    It should be something like

        SELECT *
        FROM friends
        WHERE (friend_one = ? or friend_two = ?)
        AND status = ?
    

    Of course you will need to bind the right params

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

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据