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.
$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);  
  die('Could not connect: '.mysqli_connect_error());  
echo 'Connected to database successfully<br/>';  
// mysql query to delete record
$sql = "Delete from employee where id=$id";  
//perform query operation.
if(mysqli_query($conn, $sql)){  
 echo "Record deleted successfully";  
echo "Could not deleted record: ". mysqli_error($conn);  

Connected to database successfully
Record deleted successfully

No comments:

Post a Comment