0

我有这个:

$pdo = new PDO("mysql:host=$host;dbname=$databaseName;charset=utf8", $user, $pass);
$query = "SELECT name FROM $tableName";
$stm = $pdo->prepare($query);
$stm->execute();
$array = $stm->fetchAll();
$firephp->log($array);
echo json_encode($array);

ajax 的结果是:

[{"name":"mike","0":"mike"},{"name":"john","0":"john"},{"name":"tito","0":"tito"}]

但我想得到这个:

{"name":[{"mike","john","tito"}]}

哪个 PDO fetch_style 或者我应该在 PHP 中做什么来接收我想要在 jQuery 上的字符串?

非常感谢。

4

2 回答 2

1
$array = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
echo json_encode(array('name' => $array));
于 2013-06-11T14:25:41.503 回答
0

您可以使用 PDO 获取列名:

$col_count = $stmt->columnCount();
$col_names = array();
for ($col = 0; $col < $col_count; $col++) {
    $col_names[] = $stmt->getColumnMeta($col)['name'];
}
于 2013-06-07T00:56:16.427 回答