...
$count = $conn->exec("
DELETE FROM date_practice
WHERE date between '.$dateBefor.' AND '.$dateAfter.'
");
print("Deleted $count rows.
");
} catch (PDOException $e) {
echo $sql . '<br />' . $e->getMessage();
}
$conn = NULL;
?>
It prints Deleted 0 rows.
Your query should look like this
$count = $conn->exec("DELETE FROM date_practice WHERE date between '$dateBefor' and '$dateAfter' ");
Perhaps without ''
around dates. We don't know what format you used and what your databasse accepts.
The problem is your .
s (concatenation operators) are inside your double-quotes, so they're considered part of they string. The literal query this sends will look like this:
DELETE FROM date_practice WHERE date between '.2011-01-01.' and '.2011-12-31.'
Try this:
$count = $conn->exec(
"DELETE FROM date_practice " .
" WHERE date BETWEEN '$dateBefor' AND '$dateAfter'"
);
Better yet, since you're already using PDO use prepared statements, which will worry about variable interpolation for you and protect you against SQL injection attacks:
$stmt = $dbh->prepare(
'DELETE FROM date_practice ' .
' WHERE date BETWEEN :before AND :after'
);
$stmt->bindParam(':before', $dateBefor);
$stmt->bindParam(':after', $dateAfter);
$stmt->execute();
echo 'Deleted ', $stmt->rowCount(), ' rows.';