Mlagma
Mlagma

Reputation: 1260

Why is php's fetch(PDO::FETCH_ASSOC) only grabbing the first column of my table?

The following code is a section of one of my classes:

        $stmt = $this->dbh->prepare("SELECT t_id FROM checkOut WHERE t_id = :param1");             
        $stmt->bindParam(':param1', $this->numberIn);
        $stmt->execute();
        $result = $stmt->fetch(PDO::FETCH_ASSOC);
        var_dump($result);
        $this->p_id = $result['p_id'];

My original issue was that php was stating that p_id was an undefined index. To figure out what was going on, I threw in var_dump to see what was in the array. For some reason, it contained only one value, 4 which corresponded to the first column's name, t_id. My MySQL table has four columns, and I need all four to be present in the array. Why would my code only be grabbing the first column's value?

Any help would be appreciated.

Upvotes: 0

Views: 502

Answers (2)

Marc B
Marc B

Reputation: 360572

You're only fetching one field:

SELECT t_id FROM checkOut ...
       ^^^^

if you want p_id, then you'll have to fetch that too:

SELECT p_id, t_id FROM checkOut

Upvotes: 2

Waleed Khan
Waleed Khan

Reputation: 11467

You use SELECT t_id. Use SELECT * instead.

Upvotes: 1

Related Questions