Sheriff Said Elahl
Sheriff Said Elahl

Reputation: 177

How to Select range of Dates in mysql using php prepared statement

I am working with the below prepared statement to select where exact date and it is working fine:

if($stmt = $mysqli->prepare("SELECT x FROM y WHERE mydate=?"))
{
$stmt->bind_param("s", $ReportDatetDate);
$stmt->execute();
$stmt->bind_result($xx);
while ($stmt->fetch()) 
{
echo ($xx)
}
$stmt->close();
}
else{
$mysqli->close();    
}

I needed to select where range of dates between Startdate and enddate, but I can't write the correct select query .. as below .. how can I write the correct one?

if($stmt = $mysqli->prepare("SELECT x FROM y WHERE mydate between ? AND ?"))
{
$stmt->bind_param("ss", $ReportStartDate, $reportEndDate);
$stmt->execute();
$stmt->bind_result($xx);
while ($stmt->fetch()) 
{
echo ($xx)
}
$stmt->close();
}
else{
$mysqli->close();    
}

Upvotes: 2

Views: 888

Answers (1)

ecorvo
ecorvo

Reputation: 3629

Just bind the parameters like this:

$sql = "SELECT x FROM y WHERE mydate between :from_date AND :to_date";
$stmt = $db_pdo->prepare($sql);
$stmt->execute(Array(':from_date' => $from_date, ':to_date' => $to_date));
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);

Upvotes: 1

Related Questions