gpt4 book ai didi

php - DELETE 语句循环中的 bindValue

转载 作者:行者123 更新时间:2023-11-30 22:50:49 25 4
gpt4 key购买 nike

我的 DELETE 查询方法有一个“小”问题。这段代码没有给出任何错误,所以它应该可以工作。但是数据库内部没有任何变化,我想删除的行仍然存在。

主要问题可能出在这里:

$query = $this->conn->prepare($sql);

foreach ($value as $row) {
if(is_int($row)){
$query->bindValue(':'.$field[$k].'', $row, PDO::PARAM_INT);
} else
{
$query->bindValue(':'.$field[$k].'', $row, PDO::PARAM_STR);
}
$k++;
}
$query->execute();

那个循环里面有问题。我准备好的 SQL 看起来像:从 ID = ':id' 的帐户中删除所以它看起来不错,我只是想为 :id 绑定(bind)值(如果有更多的 WHEREs 它也会绑定(bind)它们)但没有任何反应。没有错误,也没有影响。我手动检查了 foreach,$field[$k] 和 $row 返回了正确的值。我坐了4个小时。不知道出了什么问题……或者我只是瞎了眼。

此外,我在 Insert 方法中使用了完全相同的循环,一切正常。

我的代码:(抱歉代码太长)

class Delete extends Connection{

public function deleteClause($table, $where = array()){

if (count($where) % 3 === 0) {


$length = count($where) / 3;

$counter = $count = $k = 0;


for ($i = 0; $i < $length; $i++) {


for ($j = $count; $j < (count($where) / $length) + $count; $j++) {
$field[$i] = $where[$j];
$j = $j + 1;
$operator[$i] = $where[$j];
$j = $j + 1;
$value[$i] = $where[$j];
$counter = $j + 1;
}
$count = $counter;
}

$sql = "DELETE FROM $table WHERE ";

for ($i = 0; $i < $length; $i++) {
if ($i == $length - 1) {
$sql .= "$field[$i] $operator[$i] ':$field[$i]'";
} else {
$sql .= "$field[$i] $operator[$i] ':$field[$i]' AND ";
}
}
echo $sql;
$query = $this->conn->prepare($sql);


foreach ($value as $row) {
if(is_int($row)){
$query->bindValue(':'.$field[$k].'', $row, PDO::PARAM_INT);
} else
{
$query->bindValue(':'.$field[$k].'', $row, PDO::PARAM_STR);
}
$k++;
}
$query->execute();

return true;
} else {
echo "Niepoprawna ilość argumentów funkcji";
return false;
}
}}

最佳答案

使用占位符时,在您准备好的查询中,' 没有用处。像

这样的行
$sql .= "$field[$i] $operator[$i] ':$field[$i]'"

应该重写为

$sql .= "$field[$i] $operator[$i] :$field[$i]"

占位符周围没有引号 - :$field[$i],不是 ':$field[$i]'

关于php - DELETE 语句循环中的 bindValue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28178426/

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