我正在尝试从 mysql 数据中获取数据,并从中创建对象。这里我写了一些代码。我正在从数据库中获取事物的详细信息。现在,对于每个结果,我都希望创建一个最终与数组输出合并的对象。这个$output
数组最终输出为 json。
我收到错误:
array_merge(): Argument #2 is not an array
如何在 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;