dou6495 2019-01-31 11:26
浏览 49
已采纳

无法选择3个用户ID相同的表。 没有错误出现

So I have 3 tables like so

Table: users
    user_id(int)   |  phone(int)

Table: products
    prod_id(int)   |  prod_info(varchar)

Table: prod_owners
    owner_id(int)  |  user_id  |  prod_id

I created a function where I can parse in a phone number. It will look up that phone number to get the user_id. The using the user_id it will join the prod_owners table. Then using the prod_id from the prod_owners table, it will join the products table, where the user_id has the same phone number we parsed in.

However, when I run the function, I get no errors, or anything. Its just blank. I know the function works because when I do SELECT * FROM users. It works. I also know there is data with in the table (the data I want it to output).

function get_info($stmt, $phone) {

    $userID = search_user($stmt, $phone); // I feel I don't even need this
                                        // I feel I can put this in the
                                        // sql statement itself. But I am
                                        // unsure how
    if ($userID == false) {
        return "Sorry, user not found";
    }

    $sql = "SELECT u.user_id, o.prod_info 
            FROM users u 
            JOIN prod_owners o 
              USING user_id 
            JOIN products 
              USING o.prod_id 
            WHERE u.phone = ?";
    if(!mysqli_stmt_prepare($stmt, $sql)) {
        return false;
    } else {
        mysqli_stmt_bind_param($stmt, "i", $userID);
        mysqli_stmt_execute($stmt);
        $result = mysqli_stmt_get_result($stmt);
        while($row = mysqli_fetch_assoc($result)) {
          echo $row['prod_info'] . "<br>";
        }
    }
}
  • 写回答

1条回答 默认 最新

  • duanban4769 2019-01-31 11:34
    关注

    Try this query, It should must help you.

    SELECT users.user_id, products.prod_info FROM users u 
                INNER JOIN prod_owners ON prod_owners.user_id  =  users.user_id
                INNER JOIN products ON products.prod_id  =  prod_owners.prod_id
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数