最新国产好看的视频,伊人天堂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)論

灵川县| 周口市| 军事| 灵丘县| 若尔盖县| 洪湖市| 子长县| 溧水县| 荆州市| 那坡县| 贵州省| 奈曼旗| 台安县| 大庆市| 海安县| 淮安市| 德州市| 桐庐县| 蒙城县| 商城县| 泰安市| 迁安市| 长沙县| 杭锦旗| 子洲县| 甘孜县| 余姚市| 宁强县| 绍兴县| 客服| 芒康县| 吴忠市| 义马市| 进贤县| 延庆县| 南江县| 额敏县| 洪湖市| 雷州市| 榆社县| 崇义县|