一个简单的PHP服务端,输出是JSONArray
Team类
<?php
class TeamInfo{
public $name;
public $position;
public $num;
public function __construct($name,$position,$num){
$this->name = $name;
$this->position = $position;
$this->num = $num;
}
}
?>
发布数据
<?php
include 'TeamInfo.php';
#post参数
$name = $_POST ['name'];
#mysql connect 对象
$conn = mysql_connect ( "localhost", "root", "root" );
#数据库名
$database = "nbadata";
$table = "team_table";
#判断数据库读取成功
if (! $conn) {
die ( 'Could not connect: ' . mysql_error () );
}
# 选择数据库
mysql_select_db ( $database, $conn );
#Sql
#$sql_select = "select * from team_table where name = '".$name."' order by num;";
$sql_select = "select * from team_table order by num;";
$sql_result = mysql_query ( $sql_select );
$json_arr = array();
while ( $row = mysql_fetch_array ( $sql_result ) ) {
$json = new TeamInfo($row ['name'], $row ['position'], $row ['num']);
array_push($json_arr, $json);
}
$json_string = json_encode($json_arr);
echo $json_string;
#关闭数据库连接
mysql_close ( $conn );
?>