2013-08-17 107 views
-3

我想從表中刪除所有行,但它不工作。當我做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"; 
} 
} 
+1

'DELETE *'無效。你只需要'DELETE FROM ...' – Brad

+0

你可以刪除'*'和'FROM' –

回答

7

應該

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

而且我會很高興使用mysqli_*功能,而不是過時mysql_*

加,DELET不應該有* Ë;這不是正確的語法。

參考:

*語法配合使用SELECT:

+1

哦,儘管'*'可以表示全部。感謝它的工作。 –

+4

@MikelMax它用於選擇而不是刪除:) –

+0

是@ Mr.Alien你是對的。它會像'SELECT * FROM' – Gautam3164

0

如果你婉T刪除該用戶的所有消息:

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

如果你想從表中刪除所有行(小心!):

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

刪除所有行,truncate是多在較大尺寸的表上更快 – 2013-08-17 05:04:52

+0

@Dagon是的,截斷速度更快,但觸發器和約束可能存在一些問題。 – jaeheung