I have searched this question all over but could not find anything similar to this. Here is my code:
$serial = "19002817";
$start = "1443597360";
$end = "1443600000";
And here is my MySQL query:
SELECT * FROM trips WHERE serial = '$serial' AND start_date >= '$start' AND end_date <= '$end';
but then in PHP I get the following error:
Query error, 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 ''1443597360' AND end_date < '1443600000'' at line 1
Now I have noticed that it has to do with the >
and <
signs. If I remove them, then all works perfect. But I have never had this problem before.
Below is my full code
<?php
set_time_limit(0);
require_once("includes/DBC.php");
function distance($lat1, $lon1, $lat2, $lon2)
{
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
return round($miles * 1.609344, 2);
}
$serial = "19002817";
$start = "1443597360";
$end = "1443600000";
//Have tried
//$start = 1443597360;
//$end = 1443600000;
//Have tried
//$start = (int) "1443597360";
//$end = (int) "1443600000";
$Tdist = 0;
$trips = DBC::dbsql("SELECT * FROM trips WHERE serial = $serial AND start_date >= $start AND end_date <= $end;") or die(DBC::printErrors());
//Have tried
//$trips = DBC::dbsql("SELECT * FROM trips WHERE serial = ".$serial." AND start_date >= ".$start." AND end_date <= ".$end.";") or die(DBC::printErrors());
while($getTrips = DBC::dbfetch($trips))
{
$start_date = $getTripss['start_date'];
$end_date = $getTripss['end_date'];
$cor = DBC::dbsql("SELECT lat, lng FROM z_$serial WHERE datelogged BETWEEN '$start_date' AND '$end_date';");
while($getCor = DBC::dbfetch($cor))
{
$cor_lat = $getCor['lat'];
$cor_lng = $getCor['lng'];
if(!empty($first_lat) && !empty($first_lng))
{
$dist = distance($lat, $lng, $first_lat, $first_lng);
$Tdist += $dist;
}
$first_lat = $cor_lat;
$first_lng = $cor_lng;
}
print "Distance = $Tdist<br />";
}
?>