Here is my script to save and load the game for each player. It works, but I created it a long time ago, maybe there are newer methods.
<?php
//
$mysql_host = "xxxxx";
$mysql_database = "xxxxx";
$mysql_user = "xxxxx";
$mysql_password = "xxxxx";
//
$link = mysql_connect($mysql_host, $mysql_user, $mysql_password) or die("Err MySQL" );
mysql_select_db($mysql_database, $link) or die ('err db');
//
Header('Access-Control-Allow-Origin: *');
Header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
Header("Content-Type: text/html; charset=UTF-8");
$to = $_POST['to'];
//
if($to == 'savegame'){
$id_user = $_POST['id_user']; //
$gamedata = $_POST['gamedata']; //
//
$q1 = mysql_query("SELECT * FROM savegames WHERE id_user='".$id_user."' ORDER BY id_user DESC LIMIT 1");
if(mysql_num_rows($q1)==0){
//
mysql_query("INSERT INTO savegames VALUES (NULL,'".$id_user."','".$gamedata."')");
} else {
//
mysql_query("UPDATE savegames SET gamedata='".$gamedata."' WHERE id_user='".$id_user."'");
}
echo 'ok';
}
//
if($to == 'loadgame'){
$id_user = $_POST['id_user']; //
//
$q1 = mysql_query("SELECT * FROM savegames WHERE id_user='".$id_user."' ORDER BY id_user DESC LIMIT 1");
if(mysql_num_rows($q1)==0){
//
echo 'error';
} else {
//
$q2 = mysql_fetch_array($q1);
echo $q2['gamedata'];
}
}