开发者

Problems using MERGE INTO statements in php for a MySQL database

I'm trying to use a MERGE INTO statement in my php file to update or insert into a MySQL database for a multiplayer game.

Here's a full description of what I'm trying to accomplish:

The php file is called with the following line from a javascript file:

xmlhttp.open('GET', "phpsqlajax开发者_开发技巧_genxml.php?" + "lat=" + lla[0] + "&heading=" + truckHeading + "&lng=" + lla[1] + "&velocity0=" + vel0 + "&velocity1=" + vel1 + "&velocity2=" + vel2 + "&id=" + playerNumber, true);

This will be sending the php file information to update the database with. Either this will be a new player and the first time this information has been sent, meaning that a new row in the database will be created, or it will be a current player who just needs to have their information updated.

If it is a new player the "id" that is sent will be one that doesn't yet exist in the database.

For some reason the database isn't being updated, nor are new rows being added. I'm thinking it's a syntax error because I don't have much experience using MERGE statements. Could someone with experience with this please let me know what I might be doing wrong?

Here is the code before the MERGE INTO statement so you can understand which variables are which:

 $id = $_GET['id'];

 $lat = $_GET['lat'];

 $lng = $_GET['lng'];

 $heading = $_GET['heading'];

 $velocity0 = $_GET['velocity0'];

 $velocity1 = $_GET['velocity1'];

 $velocity2 = $_GET['velocity2'];

id is the column heading, $id is the id being passed in

Here is my current MERGE INTO statement in my php file:

  MERGE INTO markers USING id ON (id = $id)
  WHEN MATCHED THEN
  UPDATE SET lat = $lat, lng = $lng, heading = $heading, velocityX = $velocity0, velocityY = $velocity1, velocityZ = $velocity2
  WHEN NOT MATCHED THEN
  INSERT (id, name, address, lat, lng, type, heading, velocityX, velocityY, velocityZ)       VALUES ($id, 'bob', 'Poop Lane', $lat, $lng, 'Poop', $heading, $velocity0, $velocity1, $velocity2)


PHP's database libraries invariably have their various function calls return FALSE if anything failed during the call. Assuming you're on mysql_/mysqli_, then you shoudl be doing something like this:

$sql = "MERGE INTO ....";
$result = mysql_query($sql);
if ($result === FALSE) {
    die(mysql_error());
}

It is poor practice to NOT check the return values from database calls. Even if the query string is 100% syntactically valid, there's far too many ways for a query to fail. Assuming everything works is the easiest way to get yourself into a very bad situation. As well, when things do fail, the lack of error handling will simply hide the actual reason for the error and then you end up on SO getting answers like this.

Oh, and before I forget... MySQL doesn't support "MERGE INTO...", so your whole query is a syntax error. Look into using "REPLACE INTO..." or "INSERT ... ON DUPLICATE KEY UPDATE ..." instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜