0

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.

2 Answers 2

2

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 
Sign up to request clarification or add additional context in comments.

2 Comments

That's interesting. I was expecting that my query would locate the row associated with my parameter, so when I use fetch(PDO::FETCH_ASSOC) it would grab the entire row where t_id=whatever. Good to know, thanks.
Nope. if you don't explicitly request a field (or at least use select *), then you don't get other fields. It's a waste of disk bandwidth, cpu time, etc... to fetch data needlessly, so the db server won't get anything you don't ask for.
1

You use SELECT t_id. Use SELECT * instead.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.