I'm learning PHP and having it talk to our SQL Server 2008R2 DB. I am trying to run a query that has 2 date parameters that need to pass in. I know I could code the date params in to the SQL but I would like to see what I am doing wrong with the placeholder method below:
$serverName = "server";
$connectionInfo = array( "Database"=>"db", "UID"=>"user", "PWD"=>"pwd");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
$startDate = strtotime("2015-04-30");
$endDate = strtotime("2015-05-01");
$sql = "SELECT * FROM history
WHERE (ts >= ? and ts < ?)
ORDER BY ts";
$params = array($startDate, $endDate);
$stmt = sqlsrv_query($conn, $sql, $params);
if ($stmt == false) {
die( print_r( sqlsrv_errors(), true));
}
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
echo "<p>" . $row['vesselname'] . "</p>";
}
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
This returns the error "Arithmetic overflow error converting expression to data type datetime". ts is a datetime field.
Never mind. I figured out that I don't need the strtotime and to just pass date strings.