一個簡單的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 );
?>


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