使用日期范围从db中选择 - 致命错误:在非对象上调用成员函数fetch()

while($row = $stmt->fetch(PDO::FETCH_ASSOC))
.........

The variables are passing correctly and I have checked my database credentials and they are all correct but I am still getting the error

Fatal error: Call to a member function fetch() on a non-object

Any suggestions? Thanks in advance!

$dbh->query probably returns null if nothing is found in the database.

so only loop through the results if there actually are results

if($stmt){
  while($row = $stmt->fetch(PDO::FETCH_ASSOC))
  ...