本文介绍了PDO bindColumn和PDO :: FETCH_BOUND-是强制性还是可选的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的PHP代码的许多地方,(如果需要的话,可以与postgres一起使用)
我们有类似的东西:

In many places in our PHP code, (working with postgres if it matters)we have stuff like:

$q  = "SELECT DISTINCT a.id FROM alarms.current a, entities e, installations i ";
$q .= "WHERE i.\"entityId\"=e.id AND a.installationid=i.id AND ";
$q .= "e.id=".$entityId;

$stmt = $db->query($q);
$stmt->bindColumn("id", $alarmId);

if ($stmt->fetch(PDO::FETCH_ASSOC))
....etc

现在根据我对文档的阅读,如果要从变量的绑定列中更新变量,则应使用PDO :: FETCH_BOUND。但据我们所知,据我所知,没有人抱怨过这种性能。

Now according to my reading of the docs, if you want your variables updated from their bound columns you ought to use PDO::FETCH_BOUND. But we don't, and no-one has complained about the performance as far as I'm aware.

任何人都不能对为什么这显然是错误的代码真正产生疑问吗?

Can anyone throw any light on why this apparently faulty code actually apparently works?

推荐答案

继续@DontPanic的评论,这是我更喜欢使用绑定参数的方式:

Continuing from @DontPanic's comments, here is how I prefer to use bound parameters:

/**
* @param $id
*
* @return array|false
*/
public function retrieveImage($id)
{
    $conn = $this->dbh->getInstance('LocalMSSQL');
    $stmt = $conn->prepare("SELECT inputType, blob FROM images WHERE id = ?");

    $stmt->bindValue(1, $id, PDO::PARAM_INT);
    $stmt->execute();

    $resultSet = [
        'inputType' => null,
        'blob' => null,
    ];

    $stmt->bindColumn(1, $resultSet['inputType'], PDO::PARAM_STR);
    $stmt->bindColumn(2, $resultSet['blob'], PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_BINARY);

    if ($stmt->fetch()) {
        return $resultSet;
    } else {
        return false;
    }
}

这篇关于PDO bindColumn和PDO :: FETCH_BOUND-是强制性还是可选的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 08:16