溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

php讀取數(shù)據(jù)庫轉(zhuǎn)json數(shù)據(jù)的方法

發(fā)布時間:2020-08-13 10:02:58 來源:億速云 閱讀:361 作者:小新 欄目:編程語言

php讀取數(shù)據(jù)庫轉(zhuǎn)json數(shù)據(jù)的方法?這個問題可能是我們?nèi)粘W習或工作經(jīng)常見到的。希望通過這個問題能讓你收獲頗深。下面是小編給大家?guī)淼膮⒖純?nèi)容,讓我們一起來看看吧!

php讀取數(shù)據(jù)庫轉(zhuǎn)json數(shù)據(jù)的實現(xiàn)方法:首先連接數(shù)據(jù)庫并讀取數(shù)據(jù)庫;然后在數(shù)據(jù)庫讀取后,直接將數(shù)據(jù)轉(zhuǎn)換為數(shù)組顯示;最后通過“json_encode”轉(zhuǎn)為JSON即可。

php讀取數(shù)據(jù)庫轉(zhuǎn)json數(shù)據(jù)的方法

PHP讀取數(shù)據(jù)庫記錄轉(zhuǎn)換為JSON的代碼(API接口的SQL語句)

為了提供API接口,我們常常在讀取數(shù)據(jù)庫后,將數(shù)據(jù)轉(zhuǎn)換為數(shù)組,通過json_encode轉(zhuǎn)為JSON,即可滿足使用需要?,F(xiàn)將代碼粘帖如下:

讀取一條記錄,轉(zhuǎn)為數(shù)組并輸出JSON

include("../../db/conn.php");//數(shù)據(jù)庫連接;
echo "<pre>";
//數(shù)據(jù)庫讀取后,直接轉(zhuǎn)換為數(shù)組顯示;
$sql = "select salesid,fromstore,fromsaler,salestime,salenum,totalprice from midea_sales WHERE salesid=44";
$results = mysqli_query($con, $sql);
$rows = mysqli_fetch_assoc($results);
foreach ($rows as $key => $v) {
$res[$key] = $v;
}
echo json_encode($res);

讀取N條記錄,轉(zhuǎn)為多維數(shù)組并輸出JSON(第一種寫法)

//數(shù)據(jù)庫讀取后,直接轉(zhuǎn)換為數(shù)組顯示;
$sql = "select salesid,fromstore,fromsaler,salestime,salenum,totalprice from midea_sales";
$results = mysqli_query($con, $sql);
$data = array();//初始化數(shù)組;
class Alteration
{
public $fromstore;
public $fromsaler;
public $salenum;
public $totalprice;
}
while ($row = mysqli_fetch_assoc($results)) {
$alter = new Alteration();//實例化對象;
$alter->fromstore = $row['fromstore'];
$alter->fromsaler = $row['fromsaler'];
$alter->salenum = $row['salenum'];
$alter->totalprice = $row['totalprice'];
$data[] = $alter;
}
echo json_encode($data);

讀取N條記錄,轉(zhuǎn)為多維數(shù)組并輸出JSON(第二種寫法)

$sql = "select salesid,fromstore,fromsaler,salestime,salenum,totalprice from midea_sales";
$results = mysqli_query($con, $sql);
while ($rows = mysqli_fetch_assoc($results)) {
$res[] = $rows;
}
//$res = str_replace('[', '{', json_encode($res));
//$res = str_replace(']', '}', $res);
print_r($res);

4.讀取N條記錄,轉(zhuǎn)為多維數(shù)組并輸出JSON(第三種寫法),適合獲取全部記錄

$sql = "select salesid,fromstore,fromsaler,salestime,salenum,totalprice from midea_sales";
$results = mysqli_query($con, $sql);
$rows = mysqli_fetch_all($results);
print_r($rows);

在轉(zhuǎn)換的過程中,JSON格式會出現(xiàn)[]和{}兩種格式的JSON,而實際應用中對{}的接口是標準接口。如何轉(zhuǎn)換呢?

原因在于:當array是一個從0開始的連續(xù)數(shù)組時,json_encode出來的結(jié)果是一個由[]括起來的字符串;而當array是不從0開始或者不連續(xù)的數(shù)組時,json_encode出來的結(jié)果是一個由{}括起來的key-value模式的字符串。

$sql = "select salesid,fromstore,fromsaler,salestime,salenum,totalprice from midea_sales";
$results = mysqli_query($con, $sql);
$rows = mysqli_fetch_all($results);
$rows = str_replace('[', '{', json_encode($rows));
$rows = str_replace(']', '}', $rows);
echo json_encode($rows);

感謝各位的閱讀!看完上述內(nèi)容,你們對php讀取數(shù)據(jù)庫轉(zhuǎn)json數(shù)據(jù)的方法大概了解了嗎?希望文章內(nèi)容對大家有所幫助。如果想了解更多相關文章內(nèi)容,歡迎關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI