Reputation: 161
I have two connected tables - days
and user_days
:
DAYS:
USER_DAYS:
How I need to check if there is today (unix_date) record in user_days with user_id, and if yes then I need to get day_id after that to get data from table DAYS with day_id as ID...
I write:
public function getDay($user_id) {
$stmt = $this->conn->prepare("SELECT d.id, d.day, d.status, d.created_at, d.dayDate from days d, user_days ud WHERE d.id = ? 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);
// TODO
// $task = $stmt->get_result()->fetch_assoc();
$stmt->fetch();
$res["id"] = $id;
$res["task"] = $task;
$res["status"] = $status;
$res["created_at"] = $created_at;
$res["dayDate"] = $dayDate;
$stmt->close();
return $res;
} else {
return NULL;
}
}
on other side as index.php I have less important code:
$app->get('/days', 'authenticate', function() {
global $user_id;
$response = array();
$db = new DbHandler();
// fetch task
$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"];
echoRespnse(200, $response);
} else {
$response["error"] = true;
$response["message"] = "The requested resource doesn't exists";
echoRespnse(404, $response);
}
});
I just get this output which is not correct:
Object {error: false, id: 0, day: null, status: 0, createdAt: null}
What is wrong with my QUERY, I think that QUERY is problem but I can solve it by hours...
Upvotes: 1
Views: 352
Reputation: 3521
In getday(), I think you should replace $res["task"] = $task;
with $res["day"]=$day
.
Upvotes: 2