I am trying to check my database and return any new additions to the DB.
My client can pass in the ID of the last item it received from the DB. So i think that would be a good starting place.
So is it the case that I set the offset to the ID(theid) i pass in as an argument to the php script and that will only return Database entries newer than that id? Or am i barking up the wrong tree altogether?
Here is what I mean with code
$theid = isset($_GET['theid']) ? $_GET['theid'] : "";
$type = isset($_GET['type']) ? $_GET['type'] : "global";
$offset = isset($_GET['offset']) ? $_GET['offset'] : theid;
$count = isset($_GET['count']) ? $_GET['count'] : "100";
$sort = isset($_GET['sort']) ? $_GET['sort'] : "id ASC";
// Localize the GET variables
$udid = isset($_GET['udid']) ? $_GET['udid'] : "";
$name = isset($_GET['name']) ? $_GET['name'] : "";
$clubname = isset($_GET['clubname']) ? $_GET['clubname'] : "";
// Protect against sql injections
$type = mysql_real_escape_string($type);
$offset = mysql_real_escape_string($offset);
$count = mysql_real_escape_string($count);
$sort = mysql_real_escape_string($sort);
$udid = mysql_real_escape_string($udid);
$name = mysql_real_escape_string($name);
$clubname = mysql_real_escape_string($clubname);
// Build the sql query
$sql = "SELECT * FROM $table WHERE ";
switch($type) {
case "global":
$sql .= "1 ";
break;
case "device":
$sql .= "udid = '$udid' ";
break;
case "name":
$sql .= "name = '$name' ";
break;
case "clubname":
$sql .= "clubname = '$clubname' ";
break;
}
$sql .= "ORDER BY $sort ";
$sql .= "LIMIT $offset,$count ";
$result = mysql_query($sql,$conn);
if(!$result) {
die("Error retrieving scores " . mysql_error());
}
Many Thanks,
-Code
Assuming that the ID is a sequenced number, and newer entries will ALWAYS have a higher ID than the previous, you want the following:
Otherwise, you need to add a timestamp to your tables, and run two queries:
SELECT createdAt FROM table WHERE id = $id;
Assign that value to $timestamp and then run the following:
Optionally, you could combine the queries:
Using the offset is a bad idea. If any records prior to your ID are deleted, your results will be inaccurate. For example, lets say the following happens:
If you use an offset of 100, you will have no records returned.