9951 explained code solutions for 126 technologies


php-pdoGet key-value pairs from table using PHP PDO


$st = $pdo->prepare('SELECT id, name FROM test');
$st->execute();
$key_vals = $st->fetchAll(PDO::FETCH_KEY_PAIR);ctrl + c
$pdo->prepare

prepare given query to execute

id, name

2 columns that will be used as key (1st column) and value (2nd column)

$st->execute(

run query on the server

fetchAll

returns all rows from result set

FETCH_KEY_PAIR

will return key-value pairs based on selected columns in the resulting set