从表中删除*不起作用

时间:2013-08-17 04:47:07

标签: php mysql

我正在尝试从表中删除所有行,但它无效。当我做echo $mydb->error; 它给了我以下内容:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* FROM messages where from_user = ? and id = ?'

     $user = 'currentuser';
 if (isset($_POST['delete'])) {
 if(!empty($_POST['id'])){
 $id =  $_POST['id']; 


 $mydb = new mysqli('localhost', 'root', '', 'database');
 $stmt = $mydb->prepare("DELETE * FROM messages where from_user = ?  and id = ? ");
 echo $mydb->error;
 $stmt->bind_param('ss', $user, $id);
 foreach  ($_POST['id'] as $id) {
$stmt->execute();
}
echo"The Message deleted permanently";
}
}

2 个答案:

答案 0 :(得分:7)

应该是

$stmt = $mydb->prepare("DELETE FROM messages WHERE from_user = ?  AND id = ? ");

我会很高兴使用mysqli_*函数而不是弃用mysql_*

另外,DELETE不应该有*;这不是正确的语法。

参考:

*语法与SELECT:

一起使用

答案 1 :(得分:0)

如果要删除用户的所有消息:

$stmt = $mydb->prepare("DELETE FROM messages where from_user = ?");
$stmt->bind_param('s', $user);
$stmt->execute();

如果您想从表中删除所有行(小心!):

$stmt = $mydb->prepare("DELETE FROM messages");
$stmt->execute();