《PHP實(shí)例:php簡(jiǎn)單實(shí)現(xiàn)查詢數(shù)據(jù)庫(kù)返回json數(shù)據(jù)》要點(diǎn):
本文介紹了PHP實(shí)例:php簡(jiǎn)單實(shí)現(xiàn)查詢數(shù)據(jù)庫(kù)返回json數(shù)據(jù),希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
示例代碼一:PHP學(xué)習(xí)
// 設(shè)置返回json格式數(shù)據(jù) header('content-type:application/json;charset=utf8'); //連接數(shù)據(jù)庫(kù) $link = mysql_connect("localhost", "root", "root") or die("Unable to connect to the MySQL!"); mysql_query("SET NAMES 'UTF8'"); mysql_select_db("jilinwula", $link) or die("Unable to connect to the MySQL!"); // 獲取分頁(yè)參數(shù) $page = 0 ; $pageSize = 3; if(!is_null($_GET["page"])) { $page = $_GET["page"]; } if(!is_null($_GET["pageSize"])) { $pageSize = $_GET["pageSize"]; } // 查詢數(shù)據(jù)到數(shù)組中 $result = mysql_query("select username,password from userinfo limit " . $page . ", ". $pageSize .""); $results = array(); while ($row = mysql_fetch_assoc($result)) { $results[] = $row; } // 將數(shù)組轉(zhuǎn)成json格式 echo json_encode($results); // 關(guān)閉連接 mysql_free_result($result); mysql_close($link);
示例代碼二:PHP學(xué)習(xí)
<?php //必要執(zhí)行的SQL語(yǔ)句 //單條 $sql="select id,name from tbl_user where id=1"; //多條數(shù)據(jù) //$sql="select id,name from tbl_user"; //調(diào)用conn.php文件進(jìn)行數(shù)據(jù)庫(kù)操作 require('Conn.php'); //提示操作成功信息,注意:$result存在于conn.php文件中,被調(diào)用出來(lái) if($result) { // $array=mysql_fetch_array($result,MYSQL_ASSOC); /*數(shù)據(jù)集 $users=array(); $i=0; while($row=mysql_fetch_array($result,MYSQL_ASSOC)){ echo $row['id'].'-----------'.$row['name'].'</br>'; $users[$i]=$row; $i++; } echo json_encode(array('dataList'=>$users)); */ /*單條數(shù)據(jù)*/ $row=mysql_fetch_row($result,MYSQL_ASSOC); echo json_encode(array('jsonObj'=>$row)); } mysql_free_result($result); //釋放結(jié)果 mysql_close(); //關(guān)閉連接 ?>
以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡.PHP學(xué)習(xí)
維易PHP培訓(xùn)學(xué)院每天發(fā)布《PHP實(shí)例:php簡(jiǎn)單實(shí)現(xiàn)查詢數(shù)據(jù)庫(kù)返回json數(shù)據(jù)》等實(shí)戰(zhàn)技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培養(yǎng)人才。
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.snjht.com/jiaocheng/11035.html