-1

这是代码吗

/* already set up a query with one column in result */

$row['fk_user_pks'] = $q->fetchAll(PDO::FETCH_COLUMN);

相当于:

/* already set up a query with one column in result row */

$result = $q->fetchAll(PDO::FETCH_NUM);
$row['fk_user_pks'] = array();
foreach ($result as $r) {
     $row['fk_user_pks'][] = $r[0];
}
4

1 回答 1

3

是的,它们是等价的,如下所示:

<?php


$dbh = new PDO('sqlite::memory:');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


$dbh->exec(
    "CREATE TABLE IF NOT EXISTS `test1` ( " .
    " `id` INTEGER, " .
    " `whatever` char(10)," .
    " PRIMARY KEY (`id`) " .
    ")"
);

for ($i = 0; $i < 10; $i++) {
    $dbh->exec("INSERT INTO test1 (whatever) VALUES ('hejsan')");
}


$q = $dbh->prepare('SELECT id FROM test1');
$q->execute(array());
$ids = $q->fetchAll(PDO::FETCH_COLUMN, 0);

print_r($ids);

$q->execute(array());
$result = $q->fetchAll(PDO::FETCH_NUM);
$ids = array();
foreach ($result as $r) {
     $ids[] = $r[0];
}

print_r($ids);
于 2013-09-30T14:58:38.343 回答