[英]PHP Rest Web Service Response
我是寫Web服務的新手,似乎無法弄清楚! 我有一個簡單的php restful服務,它返回json響應,但是響應中沒有任何鍵(我不知道這是否是正確的術語)。我得到的是這樣的東西。
(
(
"name",
qty,
"image",
"price",
"date"
)
我很肯定我在這里做錯了。
這是我的服務:
<?php
header('Content-type: application/json');
class InventoryAPI {
private $db;
function __construct() {
$this->db = new mysqli('localhost', 'user', 'pass', 'dbname');
$this->db->autocommit(FALSE);
}
function __destruct() {
$this->db->close();
}
function checkInv() {
$query = "SELECT model, sku, quantity, stock_status_id, image, price, date_available FROM table_name";
$result = $this->db->query($query);
$rows = array();
$i = 0;
while($row = $result->fetch_row())
{
$rows[] = $row;
$i++;
}
$result->close();
$this->db->close();
echo json_encode($rows);
}
}
$api = new InventoryAPI;
$api->checkInv();
?>
我假設您將要使用fetch_assoc()而不是fetch_row()。 fetch_assoc()將返回一個數組,其鍵的名稱與列的名稱相同。 fetch_row()將根據列位置返回一個枚舉數組。 例如:model為0,sku為1。
您始終可以通過打印出陣列來進行測試。
while($row = $result->fetch_row())
{
print_r($row);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.