PHP Delete Data From MySQL

You can use php query to delete the data from the mysql database.

Example code:

[php]<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// sql to delete a record
$sql = "DELETE FROM MyGuests WHERE id=3";

if ($conn->query($sql) === TRUE) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $conn->error;
}

$conn->close();
?>[/php]

Code Explanation:

After we connected to the database. We have used $sql query to instruct the php script to delete the data from myGuest database WHERE id number is 3.

$sql = "DELETE FROM MyGuests WHERE id=3";

On the next statement, we have used if statement to check whether php script has successfully executed or not. if it resulted true then echo statement delete data will be executed.

Related Posts

Leave a Reply