所以我正在尝试PDO查询。看一下这个片段:

1. $rows = $stmt->fetchColumn();
2. echo $rows;
3. $row = $stmt->fetch();
4. print_r($row);


这只是一个观察...不确定如何正确!
第2行被执行并返回行数(在我的情况下为1,因为我只是在验证用户身份)。
第4行不返回任何内容。

如果您取消订单:

1. $row = $stmt->fetch();
2. print_r($row);
3. $rows = $stmt->fetchColumn();
4. echo $rows;


从第2行开始打印数组,但第4行什么也不返回!这是适当的行为吗?

无论如何,我如何获得以下结果?

if ($stmt->fetchColumn() == 1) {
    $row = $stmt->fetch();
    print_r($row);
} else {
    echo '<div class="error-message">Verify Your Credentials</div>';
}

最佳答案

这是正确的行为,fetchColumn提取一列。您无法提取两次。您可以使用:

/* fetches row from the result, if theres no row in the result, fetch returns false */
$row = $stmt->fetch();

/* compare if $row is true (in PHP, "boolean value" of unempty array is true) */
if ( $row ) {
    print_r($row);
} else {
    echo '<div class="error-message">Verify Your Credentials</div>';
}

09-30 14:57
查看更多