PHP MySQL刪除數據記錄

PHP mysql_query()函數用於刪除表中的數據記錄。 由於PHP 5.5版本起,不推薦使用mysql_query()函數。 現在,建議使用2種替代方法之一。

  • mysqli_query()
  • PDO::__query()

PHP MySQLi刪除數據記錄示例

示例

<?php  
$host = 'localhost:3306';  
$user = 'root';  // MySQL用戶賬號
$pass = '';  
$dbname = 'test';  

$conn = mysqli_connect($host, $user, $pass,$dbname);  
if(!$conn){  
  die('Could not connect: '.mysqli_connect_error());  
}  
echo 'Connected successfully<br/>';  

$id=2;  
$sql = "delete from emp4 where id=$id";  
if(mysqli_query($conn, $sql)){  
 echo "Record deleted successfully";  
}else{  
echo "Could not deleted record: ". mysqli_error($conn);  
}  

mysqli_close($conn);  
?>

執行上面示例代碼,得到以下結果 -

Connected successfully
Record deleted successfully