How do I fix this highscore php script?

Get help using Construct 2

Post » Thu Mar 24, 2016 2:13 pm

Hi Everyone,

I used to use a getscores.php and savescores.php for a online highscore system.
But on my new server, this does not work.

I get these errors:

Code: Select all
Fatal error: Uncaught Error: Call to undefined function mysql_connect()


and

Code: Select all
 Stack trace: #0 {main} thrown in


Im guessing mysql_connect() does not work anymore? i think i might be on php7

Is there anyone here who can help me you?

Here is the scripts:
getscores.php

Code: Select all
<?php
 // Date in the past
 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
 // always modified
 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
 // HTTP/1.1
 header("Cache-Control: no-store, no-cache, must-revalidate");
 header("Cache-Control: post-check=0, pre-check=0", false);
 // HTTP/1.0
header("Pragma: no-cache");
header('Access-Control-Allow-Origin: *');
$host="HOTNAME"; // Host name
$username="USERNAME"; // Mysql username
$password="PASSWORD"; // Mysql password
$db_name="MYDB"; // Database name
$tbl_name="scores"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Retrieve data from database
$sql="SELECT * FROM scores ORDER BY score DESC LIMIT 30";
$result=mysql_query($sql);

// Start looping rows in mysql database.
while($rows=mysql_fetch_array($result)){
echo $rows['name'] . "|" . $rows['score'] . "|";

// close while loop
}
// close MySQL connection
mysql_close();
?>



And here is savescores.php


Code: Select all
<?php
$db = "DBNAME";//Your database name
$dbu = "DBUSERt";//Your database username
$dbp = "PASS";//Your database users' password
$host = "HOSTNAME";//MySQL server - usually localhost
$dblink = mysql_connect($host,$dbu,$dbp);
$seldb = mysql_select_db($db);

if(isset($_GET['name']) && isset($_GET['score'])){

     //Lightly sanitize the GET's to prevent SQL injections and possible XSS attacks
     $name = strip_tags(mysql_real_escape_string($_GET['name']));
     $score = strip_tags(mysql_real_escape_string($_GET['score']));

     //Tjek om brugeren findes
     $sql = mysql_query("SELECT * FROM scores WHERE name = '".$name."' ");

     //Findes brugeren (tjekker om SQL er ok, og om der mere end 0 rækker retur)
     if($sql && mysql_num_rows($sql)>0)
     {
          //DEr findes allerede en med dette navn, shit!
          //Vi finder lige højeste score lidt kluntet.
          $highest = 0;
          while($r = mysql_fetch_array($sql))
          {
               if($r['score']>$highest)
               {
                    $highest = $r['score'];
               }
          }
          //Hvis den nye score er højere end den højeste!
          if($score>$highest)
          {
               //Ny highscore!
               mysql_query("DELETE FROM scores WHERE name = '".$name."'");
          }
     }
     //Indsæt ny highscore!
     $sql = mysql_query("INSERT INTO `$db`.`scores` (`id`,`name`,`score`) VALUES ('','$name','$score');");
     
     if($sql){
     
          //The query returned true - now do whatever you like here.
          echo 'Your score was saved. Congrats!';
         
     }else{
     
          //The query returned false - you might want to put some sort of error reporting here. Even logging the error to a text file is fine.
          echo 'There was a problem saving your score. Please try again later.';
         
     }   
}else{
     echo 'Your name or score wasnt passed in the request. Make sure you add ?name=NAME_HERE&score=1337 to the tags.';
}
mysql_close($dblink);//Close off the MySQL connection to save resources.
?>
B
37
S
9
G
8
Posts: 541
Reputation: 8,554

Post » Thu Mar 24, 2016 8:10 pm

Try removing the quotes from around the variable.

For example you have:
mysql_connect("$host", "$username", "$password")or die("cannot connect");

Believe it should be like:
mysql_connect($host, $username, $password)or die("cannot connect");

In PHP $ signifies the start of a variable. " signifies the start of a string. So "$ signifies the start of string starting with a dollar sign, not a variable.
ImageImageImage
B
40
S
23
G
11
Posts: 499
Reputation: 10,064

Post » Thu Mar 24, 2016 8:13 pm

@xanxion Did you make your avatar img? Image

I like it
ImageImageImage
B
40
S
23
G
11
Posts: 499
Reputation: 10,064

Post » Thu Mar 24, 2016 9:29 pm

I found the error, it was because im using php7 so mysql connect is deprecated.
Maybe someone can update it for php7?

The avatar is from the awesome commodore 64 game "Retrograde"
My all time favorite old-school game :D
B
37
S
9
G
8
Posts: 541
Reputation: 8,554

Post » Fri Mar 25, 2016 1:10 am

Woops, I checked and was completely wrong on the quotes. I use PHP often but stay away from putting variables inside quotes because I've been confused before on when it is acceptable.

I just checked a game I made a while ago and the high score system still works. I checked and our code is pretty identical. Can't you select what version of php you want to use on your server? in cpanel?

I'm no php whiz though. Maybe try posting on stack exchange will yield more helpful comments for you.
ImageImageImage
B
40
S
23
G
11
Posts: 499
Reputation: 10,064

Post » Fri Mar 25, 2016 10:09 am

@badmoodtaylor

Good idea to try on stack exchange!
Yes, i fixed it by selecting an older php version in .htaccess
But thats only a temp fix, since my webhost will upgrade to php7 soon.
B
37
S
9
G
8
Posts: 541
Reputation: 8,554


Return to How do I....?

Who is online

Users browsing this forum: brunopalermo, dand, tarek2, tinhofiel and 28 guests