0

I'm creating a mysql database for my android app to save and load a top 10 highscore system. I've some knowledge of php and mysql over the years however I'm no expert. Any help is appreciated:

The full code is:

<?php
define("DB_DSN",'yourdbname');
define("DB_HOST",'localhost');
define("DB_USER",'yourdblogin');
define("DB_PASS",'yourdbpass');
 
// Connecting, selecting database
$link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die('Could not connect: ' . mysql_error());
mysql_select_db(DB_DSN) or die('Could not select database');
 
if(isset($_GET)) {
    $playername = base64_decode($_GET["playername"]);
    $password = base64_decode($_GET["password"]);
    $query = 'SELECT * FROM players WHERE playername="' . mysql_real_escape_string($playername) . '" or email="' . mysql_real_escape_string($loginid) . '"';
    $dbresult = mysql_query($query, $link);
    if (!$dbresult) {
        //echo "query failed";
        $result = array();
        $result["result"] = 403;
        $result["message"] = mysql_error();
        echo json_encode($result);
        mysql_free_result($dbresult);
        exit;
    }
    $player = mysql_fetch_array($dbresult, MYSQL_ASSOC);
    if (strcmp($player["password"],md5($password)) == 0) {
        $result = array();
        $result["result"] = 200;
        $result["message"] = "Success";
        $result["playername"] = $player["playername"];
        $result["firstname"] = $player["firstname"];
        $result["lastname"] = $player["lastname"]; 
        $query = sprintf("UPDATE players SET lastlogin=NOW() WHERE id=%s;", $player["id"]);
        $uresult = mysql_query($query, $link);
        if ($uresult) {
            //code if your update failed.  Doesn't really impact what we are doing. so do nothing.
        }
        echo json_encode($result);
    } else {
        //echo "password mismatch";
        $result = array();
        $result["result"] = 403;
        $result["message"] = "Forbidden";
        echo json_encode($result);
    }
} else {
    $result = array();
    $result["result"] = 400;
    $result["message"] = "Bad Request";
    echo json_encode($result);
}
exit;

and it breaks on line 8:

$link = mysql_connect(DB_HOST, DB_USER, DB_PASS)
    or die('Could not connect: ' . mysql_error());
4
  • 1
    Is this code exactly as it's running on your server? If you've changed it, it'll be impossible for us to identify the problem since it's a Parse Error. Also, may be worth looking at mysqli, as the mysql extension is now deprecated. Commented Dec 18, 2013 at 0:08
  • @RiggsFolly there is a parse error, so mysql_error() won't ever run. Besides, mysql_error() wouldn't cause a parse error, no matter what it returned. Commented Dec 18, 2013 at 0:09
  • $link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die('Could not connect to DB'); I think you don't need mysql_error() it wouldnt be relevant in this case Commented Dec 18, 2013 at 0:11
  • 1
    Personally, I would take this time to change from PHP mysql_* functions to mysqli_* ones. It is also possible this could be at least part of the issue. Commented Dec 18, 2013 at 0:41

1 Answer 1

0

Not sure if your fixed this or not. It was a long time ago.

but this is how you could easily find errors in either your php code or your query.

  1. To start add the following to the top of your php file (right after opening [

    error_reporting(E_ALL); ini_set('display_errors', 'on');

This will show you what php errors you have on the page.

  1. Then check your query error by using

    $sql = your sql query
    $query = mysqli_query($link, "$sql") or die(mysqli_error($link);
    

The second will print the mysql error, which in turn you can then use to fix your problem .

You can always use the above to find problems.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.