简体   繁体   English

PHP查询到MySQL到JSON

[英]PHP Query to MySQL to JSON

I'm trying to write a PHP file for get user info from MySQL then parse it to JSON 我正在尝试编写一个PHP文件以从MySQL获取用户信息,然后将其解析为JSON。

So if I run link like site.com/Player/ GetFromName/Nickname 因此,如果我运行类似site.com/Player/ GetFromName / Nickname的链接

So the script will get " Nickname " user info (Coins, Rank, Gems) from MySQL For output a JSON 因此,脚本将从MySQL获取“ 昵称 ”用户信息(硬币,等级,宝石)以输出JSON

But I've no clue how to parse it. 但是我不知道如何解析它。

EDIT: There are great tutorials about PDO (database-API from PHP) with MySQL. 编辑:有关于M​​ySQL的PDO(PHP的数据库API)的出色教程。 This one is quite large but you should definitely take a look at it. 这个很大,但是您一定要看一看。 If you are able to get data from your MySQL database individually (!) you can proceed with the stuff below. 如果您能够从MySQL数据库中单独获取数据(!),则可以继续下面的内容。

Create an array of the data you got from your query. 创建从查询中获取的数据数组。 For example: 例如:

$data_array = array("name" => $name, "rank" => $rank, "coins" => $coins);


Then you can use json_encode to get your JSON-string: 然后,您可以使用json_encode来获取JSON字符串:

$json_string = json_encode($data_array);


There you have your JSON-string. 那里有您的JSON字符串。

For more information about json_encode you can visit the manual page . 有关json_encode的更多信息,您可以访问手册页

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM