douhuan1497 2014-12-09 17:03
浏览 57
已采纳

使用php mysqli确实存在数据库中的检查记录

I have some strange problem:

I have a database DAYS and USER_DAYS... DAYS: enter image description here

USER_DAYS: enter image description here

I have a function to check does today date exist in database USER_DAYS with user_id and if yes then I fetch data from DAYS table: (dbhanler.php)

public function getDay($user_id) {
        $stmt = $this->conn->prepare("SELECT d.id, d.day, d.status, d.created_at, d.dayDate, d.startTime, d.endTime from days d, user_days ud WHERE d.dayDate = ? AND ud.dayDate = d.dayDate AND ud.user_id = ?");
        $t=time();
        $dayDate = date("Y-m-d",$t);
        $stmt->bind_param("si", $dayDate, $user_id);
        if ($stmt->execute()) {
            $res = array();
            $stmt->bind_result($id, $day, $status, $created_at, $dayDate, $startTime, $endTime);

            $stmt->fetch();
            $res["id"] = $id;
            $res["day"] = $day;
            $res["status"] = $status;
            $res["created_at"] = $created_at;
            $res["dayDate"] = $dayDate;
            $res["startTime"] = $startTime;
            $res["endTime"] = $endTime;
            $stmt->close();
            return $res;
        } else {
            return NULL;
        }
    }

index.php

 $app->get('/days', 'authenticate', function() {
                global $user_id;
                $response = array();
                $db = new DbHandler();

                $result = $db->getDay($user_id);

                if ($result != NULL) {
                    $response["error"] = false;
                    $response["id"] = $result["id"];
                    $response["day"] = $result["day"];
                    $response["status"] = $result["status"];
                    $response["createdAt"] = $result["created_at"];
                    $response["dayDate"] = $result["dayDate"];
                    $response["startTime"] = $result["startTime"];
                    $response["endTime"] = $result["endTime"];

                    echoRespnse(200, $response);
                } else {
                    $response["error"] = true;
                    $response["message"] = "The requested resource doesn't exists";
                    echoRespnse(404, $response);
                }
            });

I think that I solve problem but something is very strange: When I dont have with user_id records in USER_DAYS then all work fine and give me emtpy results but if I have user_id records then give me record of wrong user, give me record of last user record submited

What can be problem here? MYSQLi QUERY?

  • 写回答

1条回答 默认 最新

  • dongse5528 2014-12-09 17:35
    关注

    The mistake is in the connection of the tables. Joining only on the day is a n to m connection, but you need a unique field to get a 1 to n connection.

    For example: USER_DAYS

    1. Data from user A, 2014-12-08
    2. Data from user B, 2014-12-08
    3. Data from user C, 2014-12-08
    4. Data from user A, 2014-12-09

    If you execute getDate(ID of user B) You will get the entries 1, 2 and 3. The function will expect only one row, so it takes the first (from user A)

    Join by the day id instead of the day

    WHERE d.dayDate = ? AND ud.day_id = d.id AND ud.user_id = ?
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

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