【问题标题】:get the index number of data gotten from mysql database获取从mysql数据库获取的数据的索引号
【发布时间】:2021-10-18 20:59:06
【问题描述】:

我正在尝试从我的 mysql 表中获取所有用户数据,并且我想检查特定数据所在位置的索引号,例如,如果数据库中有 10 个用户,并且在从中获取数据后我想检查如果任何用户 ID 与我所拥有的用户 ID 匹配,则获取他所在的索引号。如果他的数据在第 4 或第 8 位

    $userId= '****';
    $active = 'active';
    $sql = $conn->prepare("SELECT * FROM users WHERE status=? ORDER BY date DESC, class DESC");
    $sql->bind_param("s", $active);
    $sql->execute();
    $res = $sql->get_result();
    if (mysqli_num_rows($res) > 0) {
        while ($row = $res -> fetch_assoc()) {
            if($userId == $row['userId']) {

               ///Get the number he is in the array of 10 users...

            }
        }
    }
    else {
        echo json_encode("no_user");
    }

【问题讨论】:

  • “索引号”是什么意思?
  • 如果表中有自增ID列,那一列应该就是你想要的。
  • @Barmar 我使用了一个订单,所以我不能真正使用它,因为它会被混淆,我的意思是索引号是它放置在从 @ 开始的 $row 数组中的位置987654323@ 我想检查哪一个的 userId 与正在寻找的那个匹配,它的索引号是什么
  • $row 是关联的列数组,而不是行数组。

标签: php mysql arrays


【解决方案1】:

在循环中增加一个计数器变量。

        $index = 0;
        while ($row = $res -> fetch_assoc()) {
            if($userId == $row['userId']) {
               echo $index;
            }
            $index++;
        }

【讨论】:

    猜你喜欢
    • 2011-05-27
    • 1970-01-01
    • 2017-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多