2

我正在构建一个 android 游戏,我试图将用户的配置文件从 MySql 数据库下载到本地 SQLite 数据库中。我可以将配置文件发送到服务器,但无法检索它。

这是相关的android方法,它们应该没有问题,尽管逻辑本身可能已关闭,但它们可以正常运行。

    //LOAD PROFILE CLASS
    userFunctions.getServerGameState(email, monster, qty, exp);
    db.saveLocalGameSate(monster, qty, exp);
    //FUNCTIONS CLASS
    public JSONObject getServerGameState(String email, String monster, int qty, int exp) {
    // Building Parameters
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("tag", get_game_state));
    params.add(new BasicNameValuePair("email", email));
    params.add(new BasicNameValuePair("monster", monster));
    params.add(new BasicNameValuePair("qty", Integer.toString(qty)));
    params.add(new BasicNameValuePair("exp", Integer.toString(exp)));

    // getting JSON Object
    JSONObject json = jsonParser.getJSONFromUrl(monsterURL, params);
    // return json
    return json;
}
    //DB HANDLER CLASS
    public void saveLocalGameSate(String monster, int qty, int exp){

    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_MONSTER, monster); // Do they own it
    values.put(KEY_QTY, qty); // Number of things they own
    values.put(KEY_EXP, exp); // monster's level

    // Inserting Row
    db.insert(USERPROFILES_TABLE, null, values);
    db.close(); // Closing database connection

}

这是PHP,我相信我的错误在于这两个文件之一。

索引.php

 else if ($tag == 'get_game_state') {
    $email = $_POST['email'];
    $monster = $_GET['monster'];
    $qty = $_GET['qty'];
    $exp = $_GET['exp'];
    $result = $db->getGameState($email, $monster, $qty, $exp);
        if (!empty($result)) {

        echo json_encode($response);
    } else {
        $response["error"] = 1;
        $response["error_msg"] = "Error getting game state from server";
        echo json_encode($response);
    }

函数.php

public function getGameState($email, $monster, $qty, $exp) {
    $result = mysql_query("SELECT monster AND qty AND exp WHERE email = '$email'");
    $row = mysql_fetch_assoc($result);

        if (mysql_num_rows($result) > 0) {
        $gamestate = array();
        $gamestate["monster"] = $result["monster"];
        $gamestate["qty"] = $result["qty"];
        $gamestate["exp"] = $result["exp"];

        // success
        $response["success"] = 1;

        // user node
        $response["GameState"] = array();

        array_push($response["GameState"], $gamestate);

        // echoing JSON response
        echo ($response);
        } else {
            return false;
        }

这是我收到的一些错误消息

06-12 14:15:52.046: E/JSON(10577): <b>Warning</b>:  mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in 

    <b>/home/content/40/8529140/html/webapps/monster/include/DB_Functions.php</b> on line <b>145</b><br />
06-12 14:15:52.046: E/JSON(10577): <b>Warning</b>:  mysql_num_rows() expects parameter 1 to be resource, boolean given in <b>/home/content/40/8529140/html/webapps/monster/include/DB_Functions.php</b> on line <b>147</b><br />
06-12 14:15:52.046: E/JSON(10577): {"tag":"get_game_state","success":0,"error":1,"error_msg":"Error getting game state from server"}
06-12 14:15:52.046: E/JSON Parser(10577): Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject

最后,请不要告诉我我的 php 不安全,我知道这个PHP 不是我的强项,我正在寻找一个测试版本并运行。PDO在我的开发路线图上并将实施。

谢谢。

更新

修复 SQL 语句以从正确的表中选择后

06-12 15:03:48.216: E/JSON Parser(11427): Error parsing data org.json.JSONException: Value Array of type java.lang.String cannot be converted to JSONObject
4

1 回答 1

1

$result = mysql_query("SELECT monster AND qty AND exp WHERE email = '$email'");

You missed table name . It should be as follows :

$result = mysql_query("SELECT monster,qty,exp from table_name WHERE email = '$email'")
于 2012-06-12T12:55:22.167 回答