Reputation: 795
I have a MYSQL table below and I am trying to retrieve the number of records between specific dates.
I am getting the following error:
'Trying to get property 'num_rows' of non-object in /var/www/html/dev/timeTest.php on line 18'
date column of MySQL table:
PHP:
$date1 = '2018-07-01';
$date2 = '2018-07-30';
$sql = "SELECT * FROM scanUploads WHERE dateUpload BETWEEN {$date1} AND {$date2}";
$result = $mysqli -> query($sql);
$count = $result -> num_rows; // LINE 18
echo 'records: '.$count;
Upvotes: 1
Views: 471
Reputation: 76859
you are lacking quotation marks, which breaks the syntax.
and also the curly brackets appear rather strange to me.
there's two ways how one can formulate it, either delimiting the query-string with '
or with "
:
$sql = 'SELECT * FROM scanUploads WHERE dateUpload BETWEEN "'.$date1.'" AND "'.$date2.'"';
$sql = "SELECT * FROM scanUploads WHERE dateUpload BETWEEN \"$date1\" AND \"$date2\"";
// whenever wondering about the validity of generated SQL:
// die($sql);
// and whenever wondering about the result-set returned:
// die(print_r($result, true));
escaped double-quotes \"
will always be treated literally, but only within " "
double-quotes.
Upvotes: 2
Reputation: 793
$date1 = '2018-07-01';
$date1 = '2018-07-30';
$sql = "SELECT * FROM scanUploads WHERE
DATE_FORMAT(dateUpload,'%Y-%m-%d') >= '".$date1."' AND DATE_FORMAT(dateUpload,'%Y-%m-%d') <= '".$date2."' ";
$result = $mysqli -> query($sql);
$count = $result -> num_rows; // LINE 18
echo 'records: '.$count;
Upvotes: 2