0

I want to get matches played between 2 certain dates. In MySQL terminal, I hardcoded the following query and got the following rows returned (to check whether my query is correct)

QUERY

SELECT * 
FROM schedule 
WHERE gameDateTime BETWEEN '2020-02-21 00:00:00' 
                       AND '2020-02-25 15:15:00'

enter image description here

HOWEVER, when I run this simple query through PHP I get NO results

PHP CODE

$sql = "SELECT * FROM `schedule` WHERE `gameDateTime` BETWEEN '2020-02-21 00:00:00' AND '2020-02-25 15:15:00'";
    $stmnt = $db->prepare($sql);
    $games = $stmnt->fetchAll();
    foreach ($games as $game){
        echo $game['homeTeam'];
        echo 'VS';
        echo $game['awayTeam'];
        echo '<hr />';

    }

DEBUGGING

  • I DID a select(*) to ensure I can retrieve data from my DB
  • gameDateTime IS a datetiime() column

Help appreciated

Akina
  • 21,183
  • 4
  • 9
  • 16
Timothy Coetzee
  • 5,306
  • 8
  • 26
  • 84

2 Answers2

3

You're missing an $stmnt->execute()

Note that you can replace ->prepare with ->query if you wish to run the query immediately rather than preparing it first.

More info: PDO's query vs execute

Ben
  • 3,981
  • 4
  • 28
  • 49
0

You have not executed the query:

$sql = "SELECT * FROM `schedule` WHERE `gameDateTime` BETWEEN '2020-02-21 00:00:00' AND '2020-02-25 15:15:00'";
    $stmnt = $db->prepare($sql);
    $stmnt = $stmnt->execute();
    $games = $stmnt->fetchAll();
    foreach ($games as $game){
        echo $game['homeTeam'];
        echo 'VS';
        echo $game['awayTeam'];
        echo '<hr />';

    }
Dmitry
  • 2,804
  • 3
  • 13
  • 28