Hi im a complete beginer to PHP & MySQL so hop someone here can help!
I am adapting a flash game of tetris i have found on the internet and everything is fine apart from the fact it wont add the high scores & show the high scores from a MySQL database (not a text file).
im not sure what the problem is and cant seam to sort it out. the unmodified version works ok when linking to the defauld scores on his server but when you try to set it up on your own server it stops working!
if some one would be so kind to have alook at it for me i would be most greatful
the original files are here… [url]http://www.neave.com/games/tetris/tetris_fla.zip
thanks in advance
echo 'success=1&errorMsg=OK&maxScore=' . $display_max . $newline;
}
else error_msg('Could not save score.');
fclose($file);
[COLOR=Navy]else error_msg('Could not save score.');[/COLOR]
[COLOR=Navy]else error_msg('Could not save score.');[/COLOR]
[/QUOTE]
[code=php]
else {error_msg('Could not save score.');}
[/code]
[code=php]<?php
$name_max = 16; # Maximum player name length allowed
$display_max = 10; # Maximum number of scores to display (multiple of 10)
# Error handler
function error_msg($msg) {
exit("success=0&errorMsg=$msg");
}
# Store POSTed info
@$player_name = $_POST['name'];
@$player_score = $_POST['score'];
@$game_name = $_POST['game'];
# Need game name
if (!isset($game_name)) error_msg('Could not access scores.');
# Filename of text file to hold the scores given by game name - e.g. Pac-Man becomes pacman_scores.txt
$filename = $game_name . '_scores.txt';
# Saving new score?
if (isset($player_score) && is_numeric($player_score) && isset($player_name) && strlen($player_name) > 0 && strlen($player_name) <= $name_max) {
# Open the text file for writing
$file = fopen($filename, 'r+') or error_msg('Could not load scores.');
# Lock file
if (flock($file, LOCK_EX | LOCK_NB)) {
$content = fread($file, filesize($filename));
$newline = '';
$ranked = false;
$i = 1;
# Store pairs of values into pairs array
$pairs = explode('&', $content);
foreach ($pairs as $pair) {
# Store name or score pair
@list($nm, $val) = explode('=', $pair);
if ($i <= $display_max && strlen($val) > 0) {
# Insert name
if (substr($nm, 0, 4) == 'name') $this_name = $val;
else {
# Insert score or player's score
$this_score = $val;
if (!$ranked && ((int)$player_score) > ((int)$this_score)) {
$newline .= "&name$i=$player_name&score$i=$player_score";
$ranked = true;
$i++;
}
$newline .= "&name$i=$this_name&score$i=$this_score";
$i++;
}
}
}
# Write new scores
if (strlen($newline) > 0) {
ftruncate($file, 0);
rewind($file);
fwrite($file, $newline) or error_msg('Could not save score.');
}
echo 'success=1&errorMsg=OK&maxScore=' . $display_max . $newline;
}
else error_msg('2 - Could not save score.');
fclose($file);
}
else {
# Return new scores
@$file = fopen($filename, 'r') or error_msg('Could not load scores.');
$content = fread($file, filesize($filename));
echo 'success=1&errorMsg=OK&maxScore=' . $display_max . $content;
fclose($file);
}
?>[/code]
[code=php]
if ($item='condition') { execute something ; }
else { execute something different; }
[/code]
[code=php]
<?php
$name_max = 16; # Maximum player name length allowed
$display_max = 10; # Maximum number of scores to display (multiple of 10)
# Error handler
function error_msg($msg) {
exit("success=0&errorMsg=$msg");
}
# Store POSTed info
@$player_name = $_POST['name'];
@$player_score = $_POST['score'];
@$game_name = $_POST['game'];
# Need game name
if (!isset($game_name)){ error_msg('Could not access scores.'); }
# Filename of text file to hold the scores given by game name - e.g. Pac-Man becomes pacman_scores.txt
$filename = $game_name . '_scores.txt';
# Saving new score?
if (isset($player_score) && is_numeric($player_score) && isset($player_name) && strlen($player_name) > 0 && strlen($player_name) <= $name_max) {
# Open the text file for writing
$file = fopen($filename, 'r+') or error_msg('Could not load scores.');
# Lock file
if (flock($file, LOCK_EX | LOCK_NB)) {
$content = fread($file, filesize($filename));
$newline = '';
$ranked = false;
$i = 1;
# Store pairs of values into pairs array
$pairs = explode('&', $content);
foreach ($pairs as $pair) {
# Store name or score pair
@list($nm, $val) = explode('=', $pair);
if ($i <= $display_max && strlen($val) > 0) {
# Insert name
if (substr($nm, 0, 4) == 'name'){ $this_name = $val; }
else {
# Insert score or player's score
$this_score = $val;
if (!$ranked && ((int)$player_score) > ((int)$this_score)) {
$newline .= "&name$i=$player_name&score$i=$player_score";
$ranked = true;
$i++;
}
$newline .= "&name$i=$this_name&score$i=$this_score";
$i++;
}
}
}
# Write new scores
if (strlen($newline) > 0) {
ftruncate($file, 0);
rewind($file);
fwrite($file, $newline) or error_msg('Could not save score.');
}
echo 'success=1&errorMsg=OK&maxScore=' . $display_max . $newline;
}
else { error_msg('2 - Could not save score.'); }
fclose($file);
}
else {
# Return new scores
@$file = fopen($filename, 'r') or error_msg('Could not load scores.');
$content = fread($file, filesize($filename));
echo 'success=1&errorMsg=OK&maxScore=' . $display_max . $content;
fclose($file);
}
?>
[/code]
[code=php] # Write new scores
if (strlen($newline) > 0) {
ftruncate($file, 0);
rewind($file);
fwrite($file, $newline) or error_msg('Could not save score.');
}
echo 'success=1&errorMsg=OK&maxScore=' . $display_max . $newline;
}
else { error_msg('2 - Could not save score.'); }
fclose($file);
} [/code]
[code=php] else { error_msg('2 - Could not save score.'); } [/code]
> you are sending "name" but trying to get it by "username" ($_POST['username'])
> also do mysql_close(); before exit(); (in generally, you don't need exit() by the way).
0.1.9 — BETA 5.9