Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP PDOStatement: fetch a row, as the first column as the key of an array [duplicate]

I am using PDOStatement to query the database. Whenever I get a returned row, I want it to be fetched into an array, with the $row[0] as the key, and the subsequent elements in the row as the values.

I can, of course, write a combination of foreach loops and if conditionals to do the job, such as the below:

private static function GetMySQLResult($dbname, $sqlString) {          $dbh = self::ConstructPDOObject($dbname);     $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);          $result=array();     foreach ($dbh->query($sqlString) as $row) {         // the simplest case for 2 columns,          // should add more to handle more columns         $result[$row[0]][]=$row[1];     }          return $result;    } 

but I am looking for an existing method; is there such a method already exist?

Why reopened the question.

What is asked here is clearly the combination of PDO::FETCH_GROUP|PDO::FETCH_ASSOC. PDO::FETCH_KEY_PAIR only works with a 2 column result. But here the example is a 2 column result only to simplify the code in the question, not for all situations.

like image 729
Graviton Avatar asked Sep 07 '09 04:09

Graviton


People also ask

When using PHP PDOStatement fetch method the next row from a result set is retrieved?

Introduction to the PHP fetch() method Therefore, the subsequent call to the fetch() method will return the next row from the result set. To fetch all rows from a result set one by one, you typically use the fetch() method in a while loop. The fetch() method accepts three optional parameters.

What style of the fetch method can be used to return a row as an array indexed by column number?

By default, PDO returns each row as an array indexed by the column name and 0-indexed column position in the row. To request a different return style, specify one of the following constants as the first parameter when you call the PDOStatement::fetch method: PDO::FETCH_ASSOC.

What does fetchAll return PHP?

Return Values ¶ PDOStatement::fetchAll() returns an array containing all of the remaining rows in the result set. The array represents each row as either an array of column values or an object with properties corresponding to each column name. An empty array is returned if there are zero results to fetch.

Which method fetches all rows from a result as an array?

The fetch_array() / mysqli_fetch_array() function fetches a result row as an associative array, a numeric array, or both.


2 Answers

Credits go to devdRew. Check the other question here.

Apparently, you can as stated in the answer. I checked as well and it works great.

$q = $db->query("SELECT `name` AS name, `value` AS value FROM `settings`;"); $r  = $q->fetchAll(PDO::FETCH_KEY_PAIR); 

EDIT

This answer requires that you specify maximum 2 columns: 1 key and 1 value. If you need to retrieve more keys from the database, check the answer below and read @nullabilty's comment. For those who are lazy, here is his method:

$q->fetchAll(PDO::FETCH_UNIQUE); 
like image 61
Savas Vedova Avatar answered Sep 26 '22 21:09

Savas Vedova


$stmt->fetchAll(PDO::FETCH_UNIQUE);  

albeit a bit of a hack but really the only way to do this since someone decided FETCH_KEY_PAIR should require 2 column result set's only.

Note: first column is used a key and is not returned in the result set in any other form.

like image 20
Louis Avatar answered Sep 23 '22 21:09

Louis