Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

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());
share|improve this question
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. – ollieread Dec 18 '13 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. – ollieread Dec 18 '13 at 0:09
    
$link = mysql_connect(DB_HOS‌​T, 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 – meda Dec 18 '13 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. – Tigger Dec 18 '13 at 0:41

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.