I keep getting
ERROR: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near
Please help! =] I've checked that this literal query in phpmyadmin works.
SELECT *
FROM `wp_customgravityall`
WHERE (`time` >= '2015-09-01 00:00:00' AND `time` <= '2015-09-13 23:59:59')
ORDER BY `wp_customgravityall`.`time` DESC
But my code below doesn't work.
<?php
$servername = "http://www.myhomepagenamethingy.com/";
$username = DB_USER;
$password = DB_PASSWORD;
$dbname = DB_NAME;
$table_name = 'wp_' . 'customgravityall';
$from_date = isset( $_GET['from_date'] ) ? $_GET['from_date'] . " " . "00:00:00" : '';
$to_date = isset( $_GET['to_date'] ) ? $_GET['from_date'] . " " . "23:59:59" : '';
try {
$conn = new PDO('mysql:$servername;dbname=$dbname', $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$statement = $conn->prepare(
"SELECT *
FROM ?
WHERE `time` >= ? AND `time` <= ?
ORDER BY `wp_customgravityall`.`time` DESC"
);
$statement->bindValue(1, $conn->quote($table_name), PDO::PARAM_STR);
$statement->bindValue(2, $conn->quote($from_date), PDO::PARAM_STR);
$statement->bindValue(3, $conn->quote($to_date), PDO::PARAM_STR);
var_dump($statement);
$statement->execute();
} catch(PDOException $e) {
echo 'PDO ERROR: ' . $e->getMessage();
}
?>