I'm trying to build an array of objects in PHP and then echo that for use in Swift. I can get a single object to show up in Swift when I do println on the JSON object, but I need many objects echoed.
class catRow{
public $id;
public $person;
public $item;
public $descr;
public $link;
public $price;
}
$prepare_string = "SELECT * FROM items ORDER BY person LIMIT 5";
$stmt = $dbh->prepare($prepare_string);
$stmt->execute();
while($row = $stmt->fetch()){
$catRow = new stdClass;
$catRow-> id = $row['id'];
$catRow-> person = $row['person'];
$catRow-> item = $row['item'];
$catRow-> descr = $row['descr'];
$catRow-> link = $row['link'];
$catRow-> price = $row['price'];
$allRows[] = $catRow;
}
echo json_encode($allRows);
And then on the Swift side I have (summarized):
let catalogueRows: NSDIctionary = NSJSONSerialization.JSONObjectWithData(dataObject!, options: nil, error: nil) as NSDictionary
println(catalogueRows)
This works when I echo $catRow but I want to echo each row from that table in my database.
fetchfunction returns a boolean, so I don't think yourwhileloop is right. I'd callbind_resultto bind these results to variables, and then use those when definingcatRow. Or alternatively, useget_result(if you're using the MySQL native driver only), and then usefetch_assocorfetch_arrayto retrieve data for the$rowvariable like that.