PDO bindColumn和PDO :: FETCH_BOUND - 强制还是可选?

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

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?

While the example in the PHP documentation for bindColumn uses PDO::FETCH_BOUND, which does suggest that this fetch style is necessary in order to use bindColumn, it does not explicitly state that this is a requirement. It only says

PDOStatement::bindColumn() arranges to have a particular variable bound to a given column in the result-set from a query. Each call to PDOStatement::fetch() or PDOStatement::fetchAll() will update all the variables that are bound to columns.

After some testing I determined that this will occur regardless of the fetch style that is used. I think the fact that the fetch call in your code is not actually fetched into a variable really just means that an associative array is created and assigned to nothing, while the side effect of the fetch populates the $alarmId variable.

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;
    }
}