I am trying to get data from mysql data, create objects from it. Here I have written some code. I am getting thing's details from a database. Now for each result I want an object to be created which is finally merged with an array output. This $output
array is finally outputted as json.
I am getting error :
array_merge(): Argument #2 is not an array
How to create arrays of objects in PHP ?
public function getDetails() //This is a class function
{
$query = "SELECT * FROM `thing` WHERE `id` = :thingId";
$stmt = $dbh->prepare ( $query );
$stmt->bindParam ( ":thingId" , $_GET['thingId'] );
$stmt->execute ( );
$rslt = $stmt->fetch ( );
$thingName = $rslt['name'];
$thingOwnerId = $rslt['userId'];
$thingDescription = $rslt['thingDescription'];
// Getting the thing owner details
$query = "SELECT * from `user` WHERE ( `id` = :id ) ";
$stmt = $dbh->prepare( $query );
$stmt->bindParam ( ":id" , $thingOwnerId );
$stmt->execute( );
$rslt = $stmt->fetch ( );
$thingOwnerName = $rslt['firstName']." ".$rslt['lastName'];
}
$query = "SELECT * FROM `things` ; ";
$s = $dbh->prepare($query);
$s->execute();
$r = $s->fetchAll();
foreach($r as $r1)
{
$newThing = new Thingy($r1['id']);
$newThing->getDetails();
$output = array_merge($output,$newThing);
}
$output2 = json_encode($output);
header('content-type: application/json');
echo $output2;