0

如何使用 PDO 在数组中添加一行。以前我是这样做的$message = array('status' => 'ok';但是当我尝试像这样在 PDO 中使用它时它不起作用

 try {
    $dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);  
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $dbh->query($sql);  
    $employees = $stmt->fetchAll(PDO::FETCH_OBJ);
$employees['status'] = 'ok';
$employees = array($employees);
    $dbh = null;
    header('Content-Type: application/json');
    echo '{"key":'. json_encode($employees) .'}'; 
}
4

1 回答 1

0

尝试

$employees = $stmt->fetchAll(PDO::FETCH_ASSOC);

如果你想类型转换$employees成一个数组,你应该使用

$employees = (array)$employees;
于 2012-07-03T05:36:10.710 回答