So I am using PHP slimframework for just a simple API I need to use in my React application and I cannot get my query to work with these parameters, because $start_date and $end_date are not returning anything from GET request. This MySQL works like it should, I already tested it with startDate and endDate I get back from my React app, the problem is that I cannot figure it out how to get data back from these GET requests to my $start_date and $end_date variables.
This is what my backend looks like:
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
$app = new \Slim\App;
$app->get('/api/date', function(Request $request, Response $response){
$start_date=date('Y-m-d H:i:s', $_GET['startDate']);
$end_date=date('Y-m-d H:i:s', $_GET['endDate']);
// $start_date = $app->request()->params('startDate');
// $end_date = $app->request()->params('endDate');
// $start_date = $request->getAttribute['startDate'];
// $end_date = $request->getAttribute('endDate');
$sql = "SELECT * FROM `datescalendar` where `date` BETWEEN '{$start_date}' AND '{$end_date}'";
// $sql = "SELECT * FROM `datescalendar` where `date` BETWEEN '1525679047' AND '1526283847'";
try{
// Get DB Object
$db = new db();
// Connect
$db = $db->connect();
$stmt = $db->query($sql);
$dates = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
// return $response->withJson($dates);
echo json_encode($dates);
} catch(PDOException $e){
echo '{"error": {"text": '.$e->getMessage().'}';
}
});
This is how I fetch data from API in my React app:
Application is working like it should when I just query everything at once from database ($sql = "SELECT * FROM datescalendar)
Problem is definitely that $_GET just doesn’t return everything, because if If I use query with fixed startDate and endDate like this $sql = “SELECT * FROM datescalendar where date BETWEEN ‘1525679047’ AND ‘1526283847’”; then it works…
Any ideas?
I already tried this as well (it is just commented out). I am pretty sure $dates is utf8 encoded, because when I change my MySQL query to "$sql = “SELECT * FROM datescalendar;” then my application is working. So every time it fetches the whole API (I cannot get query parameters working).
I already tried my MySQL query in phpmyadmin SQL tab and in text editor with fixed sql strings I got back from my app when I click “Next Week” or “Previous Week” and it worked… I just don’t get it what could be the problem here…
// EDIT: I just found out that the problem is with my React app side logic how these parameters are changed. I am getting back valid parameters, but not for the right week. Since I am only fetching 1 week of data at the time from API I am not getting back data for the right week. Thank you for help anyway!
Also, the correct way to get get query parameters for me are:
Thank you for help, but I found out that the problem was with my React app side logic how I get these parameters. Also to get correct parameter values I had to use:
$startDate = $request->getQueryParam(‘startDate’);
$endDate = $request->getQueryParam(‘endDate’);