<?php
$db = new PDO('mysql:host=localhost;dbname=wordpress', 'root','');
//---------prepare
$delete3 = $db->prepare("DELETE FROM wp_term_relationships WHERE object_id=:id");
$delete2 = $db->prepare("DELETE FROM wp_posts WHERE ID=:id");
$delete = $db->prepare("DELETE FROM wp_postmeta WHERE post_id=:id");;
$select = $db->prepare("SELECT post_id FROM reference WHERE x_id=?");
$delete->bindParam(':id', $id, PDO::PARAM_STR);
$delete2->bindParam(':id', $id, PDO::PARAM_STR);
$delete3->bindParam(':id', $id, PDO::PARAM_STR);
//----------
echo 'conected-----';
{$delfeed = 'LOTS OF NUMBERS';}
$array = explode(',',$delfeed);
foreach($array as $deadman){
$select->execute(array($deadman));
$row = $select->Fetch(PDO::FETCH_ASSOC);
$id = $row['post_id'];
if ($id){
echo "$id"."\n";
$delete->execute();
$delete2->execute();
$delete3->execute();
}}
echo 'done!';
?>
它是一个简单的删除脚本,但它不会删除,它确实打印正确的$ id的女巫意味着工作直到那里但删除去香蕉,双重检查表名称,colums ... tryied working with问号所在者持有绑定参数但没什么 更新:它停留在foreach
中数组的第一项答案 0 :(得分:2)
问题是您在创建$id
变量之前设置它。
<?php
$db = new PDO('mysql:host=localhost;dbname=wordpress', 'root','');
$delete3 = $db->prepare("DELETE FROM wp_term_relationships WHERE object_id=?");
$delete2 = $db->prepare("DELETE FROM wp_posts WHERE ID=?");
$delete = $db->prepare("DELETE FROM wp_postmeta WHERE post_id=?");
$select = $db->prepare("SELECT post_id FROM reference WHERE x_id=?");
foreach($array as $deadman){
$select->execute(array($deadman));
$row = $select->Fetch(PDO::FETCH_ASSOC);
$id = $row['post_id'];
if ($id) {
echo "$id"."\n";
$data = array($id);
$delete->execute($data);
$delete2->execute($data);
$delete3->execute($data);
}
$id++;
}
echo 'done!';
?>
我确实更新了你的代码,因为有些位没有意义,比如$delfeed
是一个空字符串,增加了$id
变量,即使它被覆盖了,并且有随机的大括号放在一边。但正如您所看到的,在if($id)
的检查中,我将变量绑定在那里。
您可以将文档顶部的变量$id
指定为0,而不是使用我的方法;然后,在完成所有准备语句之后,您可以使用bindParam
函数代替bindValue
。
$id = 0;
// Prepare Statements
$delete->bindParam(':id', $id, PDO::PARAM_STR);
$delete2->bindParam(':id', $id, PDO::PARAM_STR);
$delete3->bindParam(':id', $id, PDO::PARAM_STR);
// Rest of Original Code With No Changes
这样做的原因是因为bindParam
在查询执行时通过引用传递。请参阅PHP bindParam Document以及this answer.
编辑:解决方案2修复Foreach问题
<?php
$db = new PDO('mysql:host=localhost;dbname=wordpress', 'root','');
$delete3 = $db->prepare("DELETE FROM wp_term_relationships WHERE object_id=:id");
$delete2 = $db->prepare("DELETE FROM wp_posts WHERE ID=:id");
// Had a double semicolon trailing the function. Removed one of them
$delete = $db->prepare("DELETE FROM wp_postmeta WHERE post_id=:id");
$select = $db->prepare("SELECT post_id FROM reference WHERE x_id=?");
$id = 0; // Define Variable BEFORE bindParam
$delete->bindParam(':id', $id, PDO::PARAM_STR);
$delete2->bindParam(':id', $id, PDO::PARAM_STR);
$delete3->bindParam(':id', $id, PDO::PARAM_STR);
$delfeed = ''; // Why Have Curly Braces around this?
$array = explode(',',$delfeed);
foreach($array as $deadman) {
$select->execute(array($deadman));
$row = $select->Fetch(PDO::FETCH_ASSOC);
$id = $row['post_id'];
if ($id){
echo "$id"."\n";
$delete->execute();
$delete2->execute();
$delete3->execute();
}
}
echo 'done!';
?>