I am new to writing web services and I cannot seem to figure this out! I have a simple php restful service that returns a json response, but I don't have any keys in the response (I don't know if that's the right term) What I get is something like this.
(
(
"name",
qty,
"image",
"price",
"date"
)
I am positive I am doing something wrong here.
Here is my service:
<?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();
?>
I'm assuming you will want to use fetch_assoc() instead of fetch_row(). fetch_assoc() will return an array with keys the same name as the columns. fetch_row() will return an enumerated array based on the column position. For example: model is 0, sku is 1.
You can always test this out by printing out the array.
while($row = $result->fetch_row())
{
print_r($row);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.