How do I fix this highscore php script?

  • 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:

    Fatal error: Uncaught Error: Call to undefined function mysql_connect() [/code:91zd5q7n]
    
    and
    
    [code:91zd5q7n] Stack trace: #0 {main} thrown in[/code:91zd5q7n]
    
    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:91zd5q7n]<?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();
    ?>[/code:91zd5q7n]
    
    And here is savescores.php
    
    [code:91zd5q7n]<?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.
    ?>[/code:91zd5q7n]
  • 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.

  • xanxion Did you make your avatar img?

    I like it

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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

  • 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.

  • 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.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)