gpt4 book ai didi

php - 使用 PDO 和 mySQL 返回单个值的正确语法是什么?

转载 作者:行者123 更新时间:2023-11-29 05:58:02 24 4
gpt4 key购买 nike

我正在尝试将我的 sql 切换到 PDO,但我什至无法让这个简单的查询返回值。肯定不会这么难。我以老式的方式完成了相同的查询,并且它按预期完美运行。 PDO 版本不返回任何内容。这里有什么诀窍?

第一个版本返回我期望的值。

  $customfieldid = 676;
$entityid = 9784549;
$entitytype = 'familyoverview';

$sql = "select value
from customfieldvalues
where customfieldid = ".$customfieldid."
and entityid = ".$entityid."
and entitytype = '".$entitytype."'";

$result = mysql_query($sql);

if($row = mysql_fetch_array($result)) {
$mysqlvalue = $row["value"];
echo "<br>mysql value: ".$mysqlvalue;
}

此 PDO 版本不返回任何内容。

  $sql = "select value
from customfieldvalues
where customfieldid = :customfieldid
and entityid = :entityid
and entitytype = :entitytype";

$stmt = $pdo->prepare($sql);
//$stmt->bindValue(':customfieldid', $customfieldid, PDO::PARAM_INT);
//$stmt->bindValue(':entityid', $entityid, PDO::PARAM_INT);
//$stmt->bindValue(':entitytype', $entitytype, PDO::PARAM_STR);

$stmt->bindValue(':customfieldid', 676, PDO::PARAM_INT);
$stmt->bindValue(':entityid', 9784549, PDO::PARAM_INT);
$stmt->bindValue(':entitytype', 'familyoverview', PDO::PARAM_STR);

$pdovalue = $stmt->fetchColumn();
echo "<br>pdo value: ".$pdovalue;

我已经确认我有一个 pdo 数据库连接。我试过使用第三个参数并在 bindValue 调用中省略第三个参数。我也尝试过对 bindValue 调用中的值进行硬编码,而不是将它们传入,但这些都没有任何区别。

最佳答案

您的 PDO 代码缺少对 $stmt->execute() 的调用,因此查询永远不会发送到 MySQL 服务器并执行。不执行查询,就不会有任何结果。

关于php - 使用 PDO 和 mySQL 返回单个值的正确语法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47542833/

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