php簡單實現查詢資料庫返回json資料,php資料庫返回json_PHP教程

來源:互聯網
上載者:User

php簡單實現查詢資料庫返回json資料,php資料庫返回json


範例程式碼一:

// 設定返回json格式資料header('content-type:application/json;charset=utf8');//串連資料庫$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!");// 擷取分頁參數$page = 0 ;$pageSize = 3;if(!is_null($_GET["page"])) {$page = $_GET["page"];}if(!is_null($_GET["pageSize"])) {$pageSize = $_GET["pageSize"];}// 查詢資料到數組中$result = mysql_query("select username,password from userinfo limit " . $page . ", ". $pageSize ."");$results = array();while ($row = mysql_fetch_assoc($result)) {$results[] = $row;}// 將數組轉成json格式echo json_encode($results);// 關閉串連mysql_free_result($result);mysql_close($link);

範例程式碼二:

<?php//需要執行的SQL語句//單條$sql="select id,name from tbl_user where id=1";//多條資料//$sql="select id,name from tbl_user";//調用conn.php檔案進行資料庫操作 require('Conn.php'); //提示操作成功資訊,注意:$result存在於conn.php檔案中,被調用出來 if($result) { // $array=mysql_fetch_array($result,MYSQL_ASSOC);   /*資料集 $users=array(); $i=0; while($row=mysql_fetch_array($result,MYSQL_ASSOC)){  echo $row['id'].'-----------'.$row['name'].'
'; $users[$i]=$row; $i++; } echo json_encode(array('dataList'=>$users)); */ /*單條資料*/ $row=mysql_fetch_row($result,MYSQL_ASSOC); echo json_encode(array('jsonObj'=>$row));} mysql_free_result($result);//釋放結果mysql_close();//關閉串連?>

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

http://www.bkjia.com/PHPjc/985277.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/985277.htmlTechArticlephp簡單實現查詢資料庫返回json資料,php資料庫返回json 範例程式碼一: // 設定返回json格式資料header('content-type:application/json;charset=utf8');//串連...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.