1

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:

enter image description here

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;
6
  • use $date1 = '2018-07-01 00:00:00'; $date1 = '2018-07-30 23:59:59'; Commented Jul 30, 2018 at 9:54
  • Is that possible that $result is null? Commented Jul 30, 2018 at 9:56
  • @DevsiI, am not sure that I have the correct SQL syntax? I have tried add '00:00:00' to the search dates but the error message is the same. Commented Jul 30, 2018 at 9:58
  • Is it allowed to put spaces after you want an object class? $result -> num_rows; should it be this? $result->num_rows; Commented Jul 30, 2018 at 10:10
  • you code is unsecure, try php.net/manual/en/pdostatement.rowcount.php Commented Jul 30, 2018 at 10:15

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2
$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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.