Articles

PHP MySQL - DELETE

PHP MySQL - DELETE


Let’s discuss how to delete records in MYSQL through PHP  in the XAMPP Server.

Consider the table - Medicine present in the database - hospital snapshot with existing records.

 

DELETE in MySQL is used to delete the rows based on the condition specified inside WHERE clause.

 

SQL DELETE- Syntax

 

DELETE FROM TABLE_NAME WHERE conditions….

As we know that columns refers to the column names that exist in the given table (TABLE_NAME).

 

Example 1:

In this example, we will delete rows where Cost is 200.

<?php

// Specify the server
$server = "localhost";

// Specify the user
$user = "root";

// Specify the password
$pwd = "";

// Specify the database 
$data = "hospital";

// Let's create connection by using the above details
$connecting_data =  new mysqli($server, $user, $pwd, $data);

$my_query = "DELETE FROM Medicine WHERE Cost =200";


if ($connecting_data->query($my_query) === TRUE) {
  echo "Deleted Succesfully!!.";
} else {
  echo "Error: " . $connecting_data->error;
}

$connecting_data->close();
?>

Output:

Let's check in XAMPP:

So we can see that records were deleted.

 

Example 2:

In this example, we will delete rows where Tablet is 'Vicks'.

<?php

// Specify the server
$server = "localhost";

// Specify the user
$user = "root";

// Specify the password
$pwd = "";

// Specify the database 
$data = "hospital";

// Let's create connection by using the above details
$connecting_data =  new mysqli($server, $user, $pwd, $data);

$my_query = "DELETE FROM Medicine WHERE Tablet ='Vicks'";


if ($connecting_data->query($my_query) === TRUE) {
  echo "Deleted Succesfully!!.";
} else {
  echo "Error: " . $connecting_data->error;
}

$connecting_data->close();
?>

Output:

Let's check in XAMPP:

So we can see that records were deleted.

 

Example 3:

In this example, we will delete rows where Diesese is 'Nose pain' and Quantity = 1.

<?php

// Specify the server
$server = "localhost";

// Specify the user
$user = "root";

// Specify the password
$pwd = "";

// Specify the database 
$data = "hospital";

// Let's create connection by using the above details
$connecting_data =  new mysqli($server, $user, $pwd, $data);

$my_query = "DELETE FROM Medicine WHERE Disease ='Nose pain' AND Quantity= 1";


if ($connecting_data->query($my_query) === TRUE) {
  echo "Deleted Succesfully!!.";
} else {
  echo "Error: " . $connecting_data->error;
}

$connecting_data->close();
?>

Output:

Let's check in XAMPP:

 

So we can see that record is deleted.

 

Conclusion:

In this tutorial, we seen three different examples to delete rows from a table through PHP Script in XAMPP Server.

 


PHP MySql

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University. Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :    Published Date : Jun 14,2024  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!