gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 13:54:43 27 4
gpt4 key购买 nike

在我们的 PHP 代码中的许多地方,(如果重要的话使用 postgres)我们有这样的东西:

$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

现在根据我对文档的阅读,如果您希望从它们的绑定(bind)列更新您的变量,您应该使用 PDO::FETCH_BOUND。但我们没有,据我所知,没有人提示性能。

谁能解释为什么这个明显错误的代码实际上有效?

最佳答案

虽然 bindColumn 的 PHP 文档中的示例使用 PDO::FETCH_BOUND,这确实表明这种获取样式是使用 bindColumn< 所必需的,它没有明确说明这是一项要求。它只是说

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.

经过一些测试后,我确定无论使用何种提取样式,都会发生这种情况。我认为您的代码中的 fetch 调用实际上并未提取到变量中,这实际上只是意味着创建了一个关联数组并将其分配给任何内容,而提取的副作用填充了 $alarmId 变量。

关于php - PDO bindColumn 和 PDO::FETCH_BOUND——强制的还是可选的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34438490/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com