最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

php簡(jiǎn)單實(shí)現(xiàn)查詢數(shù)據(jù)庫(kù)返回json數(shù)據(jù)

 更新時(shí)間:2015年04月16日 10:40:54   投稿:hebedich  
這篇文章主要介紹了php簡(jiǎn)單實(shí)現(xiàn)查詢數(shù)據(jù)庫(kù)返回json數(shù)據(jù),并附上2則示例代碼,非常的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下。

示例代碼一:

// 設(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

//需要執(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)容了,希望大家能夠喜歡。

相關(guān)文章

最新評(píng)論

宣恩县| 漳州市| 武定县| 济南市| 沅陵县| 建水县| 天水市| 淄博市| 泰兴市| 许昌市| 长沙县| 冀州市| 谢通门县| 萨迦县| 区。| 麻栗坡县| 盐津县| 奉新县| 汉源县| 文水县| 通海县| 海林市| 横山县| 威宁| 手游| 濮阳市| 江门市| 西林县| 石阡县| 从化市| 股票| 浪卡子县| 靖边县| 大方县| 东阿县| 鄄城县| 林口县| 大荔县| 合水县| 揭西县| 武义县|