سؤال

I'm using PHP PDO to access a sql database. There's a table with just two columns (ID and VALUE). I want to read that table into an array such that

$array[ID]=VALUE

I know how I can do it manually with a for loop or while loop going through one by one… but I was wondering if there's any better way of doing it.

هل كانت مفيدة؟

المحلول

You can use PDO::FETCH_KEY_PAIR constant

$sql  = "select id, username from users limit 10";
$data = $pdo->query($sql)->fetchAll(PDO::FETCH_KEY_PAIR);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top