jimmy
jimmy

Reputation: 411

PDO::FETCH_COLUMN fetches nothing

The following code gives me an array $a containing description.

$stmt = $dbh->prepare("SELECT description, id
    FROM money_items");
$stmt->execute();
$a = $stmt->fetchAll(PDO::FETCH_COLUMN, 0);
$id = $stmt->fetchAll(PDO::FETCH_COLUMN, 1);

But the $id array comes out blank.

   Array
   (
   )

If I remove the $a = $stmt->fetchAll(PDO::FETCH_COLUMN, 0); line, the $id array comes out fine. Am I only allowed to fetch one column from the results?

Upvotes: 0

Views: 4405

Answers (1)

Toby Allen
Toby Allen

Reputation: 11213

what you are looking for is

$results = $stmt->fetchAll(PDO::FETCH_ASSOC); 

This will fetch all columns and all rows to an array -

http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers

To answer your actual question though, I would guess that the FetchAll function called on the statement runs through all the rows, so the internal pointer in the array is at the end when you fetch the second column.

Upvotes: 1

Related Questions