Wednesday, October 11, 2017

PHP Delete Data From MySQL Database

In this article, we are going to learn how to delete records in database table using PHP Script. To delete records in database table we are using PHP mysqli_query() function. This function is used to perform query operation in MySQL database.


NOTE : mysql_query() function is deprecated, since PHP 5.5 Version.
mysqli_connect() function is not available form PHP 7




PHP MySQLi Delete Record Example

Let see the simple example to delete record from MySQL database using mysqli_query() function.
<?php  
$host = 'localhost'; // set the hostname
$user = '';  // set the mysql username
$pass = '';  //set the mysql password
$dbname = 'companyrecord';  
//establish the database connection  
$conn = mysqli_connect($host, $user, $pass,$dbname);  
if(!$conn){  
  die('Could not connect: '.mysqli_connect_error());  
}  
echo 'Connected to database successfully<br/>';  
  
$id=1;  
// mysql query to delete record
$sql = "Delete from employee where id=$id";  
//perform query operation.
if(mysqli_query($conn, $sql)){  
 echo "Record deleted successfully";  
}else{  
echo "Could not deleted record: ". mysqli_error($conn);  
}  
  
mysqli_close($conn);  
?>

Output:
--------------------
Connected to database successfully
Record deleted successfully


No comments:

Post a Comment