【问题标题】:PHP Rest Web Service ResponsePHP Rest Web 服务响应
【发布时间】:2014-04-17 17:04:21
【问题描述】:

我是编写 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();

?>

【问题讨论】:

    标签: php json web-services rest


    【解决方案1】:

    我假设你会想要使用 fetch_assoc() 而不是 fetch_row()。 fetch_assoc() 将返回一个具有与列同名的键的数组。 fetch_row() 将根据列位置返回一个枚举数组。例如:model 为 0,sku 为 1。

    您总是可以通过打印出数组来测试这一点。

    while($row = $result->fetch_row())
        {
            print_r($row);
        }
    

    【讨论】:

      猜你喜欢
      • 2011-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-21
      • 2011-04-22
      • 1970-01-01
      • 1970-01-01
      • 2014-07-30
      相关资源
      最近更新 更多