亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

示例代碼一:

// 設(shè)置返回json格式數(shù)據(jù)
header('content-type:application/json;charset=utf8');

//連接數(shù)據(jù)庫
$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!");

// 獲取分頁參數(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語句
//單條
$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ù)庫操作 
require('Conn.php'); 

//提示操作成功信息,注意:$result存在于conn.php文件中,被調(diào)用出來 
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)閉連接

?>

以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。

相關(guān)文章

最新評論