php - JSON获取特定用户的高分

标签 php android mysql json

我想在我的应用程序中显示每个用户的分数,这个分数是所有操作的结果,他们会因评论主题等获得 2 分。

我在数据库中创建了一个新表“users”,其中包含 2 行、号码和用户名。 这是添加分数和用户名的 php 文件:

<?php

//load and connect to MySQL database stuff
require("config.inc.php");

if (!empty($_POST)) {
    //initial query
    $query = "INSERT INTO users ( score, username ) VALUES ( :nu, :user) ";

    //Update query
    $query_params = array(
        ':nu' => $_POST['score'],
        ':user' => $_POST['username']

    );

    //execute query
    try {
        $stmt   = $db->prepare($query);
        $result = $stmt->execute($query_params);
    }
    catch (PDOException $ex) {
        // For testing, you could use a die and message. 
        //die("Failed to run query: " . $ex->getMessage());

        //or just use this use this one:
        $response["success"] = 0;
        $response["message"] = "Database Error. Couldn't add post!";
        die(json_encode($response));
    }

    $response["success"] = 1;
    $response["message"] = "Username Successfully Added!";
    echo json_encode($response);

} else {
?>
        <h1>Add Score</h1> 
        <form action="addscore.php" method="post"> 

            Username:<br /> 
            <input type="text" name="username" placeholder="post username" /> 
            <br /><br />


            Score:<br /> 
            <input type="text" name="score" placeholder="post score" /> 
            <br /><br />
            <input type="submit" value="Add Score" /> 
        </form> 
    <?php
}

?>  

以 JSON 格式显示用户名和分数的代码:

<?php

/*
Our "config.inc.php" file connects to database every time we include or require
it within a php script.  Since we want this script to add a new user to our db,
we will be talking with our database, and therefore,
let's require the connection to happen:
*/
require("config.inc.php");

//initial query
$query = "Select * FROM users";

//execute query
try {
    $stmt   = $db->prepare($query);
    $result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
    $response["success"] = 0;
    $response["message"] = "Database Error!";
    die(json_encode($response));
}

// Finally, we can retrieve all of the found rows into an array using fetchAll 
$rows = $stmt->fetchAll();

if ($rows) {


    $response  = array();


    foreach ($rows as $row) {
        $post             = array();



        $post["score"] = $row["score"];
        $post["username"] = $row["username"];



        //update our repsonse JSON data
        array_push($response, $post);
    }

    // echoing JSON response
    echo json_encode($response);


} else {
    $response["success"] = 0;
    $response["message"] = "No Post Available!";
    die(json_encode($response));
}

?>

如何才能仅获取特定用户的分数? 每个用户都应该在他的个人资料中看到他的个人分数。

最佳答案

您可以更改您的查询

Select * FROM users

Select * FROM users where id = '1'

关于php - JSON获取特定用户的高分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29259649/

相关文章:

android - 按回不返回到上一个 fragment

android - 在android中检测VoLTE调用

android - cordova push 和本地通知冲突

PHP时差类返回不同时差

mysql - 使用python无法将CSV文件加载到MySql中

php - 将输入类型文本的数据数组保存到数据库

php - 将 URL 中的下划线替换为斜杠并剪切扩展名

curl - php webpayments pro install.php 不工作

php - 对象分配在 PHP OOP 中没有意义

mysql - 使用相同的id更新多个表的记录