简体   繁体   English

PDO Fetch 语句只检索第一列

[英]PDO Fetch statement only retrieves first column

As the title states, I am only able to grab the id column, I am getting an Undefined index error for the others.正如标题所述,我只能获取 id 列,其他人收到未定义的索引错误。

My code:我的代码:

if(isset($_SESSION['id'])) {
    $presh = $_SESSION['id'];
    $stmt = $pdo->prepare("SELECT id FROM users WHERE id = :id");
    $id = $presh;
    $stmt->execute(array(':id'=>$id));
    $accountinfo = $stmt->fetch(PDO::FETCH_ASSOC);
}

Later on in my code I reference it as such:后来在我的代码中,我这样引用它:

Karma <span id="kcurrent"><?php echo $accountinfo["karmacurrent"]; ?></span> | <span id="ktotal"><?php echo $accountinfo["karmatotal"]; ?></span>

The rows do exist and they are filled, what am I doing wrong?这些行确实存在并且它们已被填充,我做错了什么?

You can only select the id column because that is all you have in the query.您只能选择id列,因为这是您在查询中的全部内容。

Try something like this尝试这样的事情

if (isset($_SESSION['id'])) {
    $presh = $_SESSION['id'];
    $stmt = $pdo->prepare("SELECT id, karmacurrent FROM users WHERE id = :id");

    $id = $presh;

    $stmt->execute(
        array(
            ':id'=>$id
        )
    );

    $accountinfo = $stmt->fetch(PDO::FETCH_ASSOC);
}

Basically you were only retrieving the ID from the table instead of the other columns.基本上,您只是从表而不是其他列中检索ID

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

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