gpt4 book ai didi

php - 如何将动态参数传递给查询?

转载 作者:行者123 更新时间:2023-11-29 04:34:06 25 4
gpt4 key购买 nike

这是我的代码:

$param2 = $val2 = "";
if ($myCondition){
$parm2 = ", param2 = ?";
$val2 = "something";
}

$stmt = $dbh_conn->prepare("UPDATE myTable SET param1 = ? $param2 WHERE param3 = ?");
$stmt->execute([$val1, $val2, $val3]);

当该条件为 true 时,我的代码有效。当它为 false 时我该如何处理?

最佳答案

$param2 = $val2 = "";
if ($myCondition){
$parm2 = ", param2 = ?";
$val2 = "something";
$stmt = $dbh_conn->prepare("UPDATE myTable SET param1 = ? $param2 WHERE param3 = ?");
$stmt->execute([$val1, $val2, $val3]);
} else {
$stmt = $dbh_conn->prepare("UPDATE myTable SET param1 = ? WHERE param3 = ?");
$stmt->execute([$val1, $val3]);
}

关于php - 如何将动态参数传递给查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49155922/

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