9951 explained code solutions for 126 technologies


php-pdoFetch array of column values with PHP PDO


$st = $pdo->prepare('SELECT id FROM test');
$st->execute();
$cols = $st->fetchAll(PDO::FETCH_COLUMN);ctrl + c
$pdo->prepare

prepare given query to execute

id

column we want to get into resulting array

$st->execute(

run query on the server

fetchAll

returns all rows from result set

FETCH_COLUMN

will return single column value per row rather than associative array

$cols

resulting array of column values