一个简单的Php服务端

一个简单的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 );
?>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章