简体   繁体   English

为什么php的fetch(PDO :: FETCH_ASSOC)只获取表的第一列?

[英]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. 我最初的问题是php声明p_id是未定义的索引。 To figure out what was going on, I threw in var_dump to see what was in the array. 为了弄清楚发生了什么,我投入了var_dump来查看数组中的内容。 For some reason, it contained only one value, 4 which corresponded to the first column's name, t_id . 由于某种原因,它仅包含一个值4 ,它对应于第一列的名称t_id My MySQL table has four columns, and I need all four to be present in the array. 我的MySQL表有四列,我需要在数组中全部包含四列。 Why would my code only be grabbing the first column's value? 为什么我的代码只能获取第一列的值?

Any help would be appreciated. 任何帮助,将不胜感激。

You're only fetching one field: 您只获取一个字段:

SELECT t_id FROM checkOut ...
       ^^^^

if you want p_id, then you'll have to fetch that too: 如果您想要p_id,那么您也必须获取它:

SELECT p_id, t_id FROM checkOut

You use SELECT t_id . 您使用SELECT t_id Use SELECT * instead. 请改用SELECT *

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM