I am updating my code to use PHP PDO... I came across an sql query
$sql = "SELECT COUNT(id) AS number_of_item FROM ".$db_table_prefix."item_table
WHERE id > 0
AND date_visited BETWEEN
CAST('$start_date' AS DATE )
AND CAST('$end_date' AS DATE )";
which i replace with this
$this->sql = "SELECT COUNT(id) AS number_of_items FROM item_table
WHERE id > :id AND date_visited
BETWEEN CAST(:start_date AS DATE )
AND CAST(:end_date AS DATE )";
$this->prepare($this->sql);
$this->bind(':id', 0);
$this->bind(':start_date', $date_start);
$this->bind(':end_date', $date_end);
$this->execute();
sizeof($this->multiple_fetch()) > 0 ? $this->result_set = $this->multiple_fetch() : $this->result_set = 404;
return $this->result_set;
I have entries in my database that meet all the criteria but my new code is giving this as a result
array (size=1) 0 =>
array (size=1)
'number_of_items' => int 0
My questions, is there something i am doing wrong? Is there a better way or different way of using the
CAST(... AS DATE)
in PDO?
UPDATE
Here is what my bind method look like
function bind($placeholder, $value, $type = null){
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = \PDO::PARAM_INT;
break;
case is_bool($value):
$type = \PDO::PARAM_BOOL;
break;
case is_null($value):
$type = \PDO::PARAM_NULL;
break;
default:
$type = \PDO::PARAM_STR;
}
}
$this->stmt->bindValue($placeholder, $value, $type);
}
My $date_start and date_end values are 2016-11-01 and 2016-11-30 respectively. I have about 101 dummy entries in my db (item_table) with date_visited as 2016-11-18
$start_dateand$end_date? Also in youbind()method, are you passing them asstring?$start_dateand$end_dateDateTimeobjects orstring? Have you tried$start_date = date('Y-m-d', strtotime($start_date))(and same for$end_date) and then pass them to the query withoutCAST?date('Y-m-d', strtotime($start_date))and i am still getting same result... The$start_dateand$end_dateare both string...mysqldo you fetch records?date('Y-m-d', strtotime($start_date))thing before binding, i don't know how that affect it? but when i did this$this->bind(':start_date', date("Y-m-d", strtotime($date_start))); $this->bind(':end_date', date("Y-m-d", strtotime($date_end)));it worked. and i also remove the CAST (), so my query look like thisAND date_visited BETWEEN :start_date AND :end_dateplease give that as an answer. Thanks alot