how to delete a row in mysql using php?

Today, We want to share with you mysql delete.In this post we will show you Deleting Data from a Table – MySQL Tutorial, hear for Delete Data From a MySQL Table Using MySQLi and PDO we will give you demo and example for implement.In this post, we will learn about Insert Update Delete using PHP and mysql for Frontaccounting – crud with an example.

PHP MySQL Delete Data

Syntax

DELETE [ LOW_PRIORITY ] [ QUICK ] [ IGNORE ] FROM table
[WHERE conditions]
[ORDER BY expression [ ASC | DESC ]]
[LIMIT number_rows];

Example (MySQLi Object-oriented)

<?php
$hosting_name = "localhost";
$username = "main_user_version";
$password = "password";
$dbname = "pakainfo";

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

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

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

$db_connect->close();
?>

Example (MySQLi Procedural)

<?php
$hosting_name = "localhost";
$username = "main_user_version";
$password = "password";
$dbname = "myDB";

// Create connection
$db_connect = mysqli_connect($hosting_name, $username, $password, $dbname);
// Check connection
if (!$db_connect) {
  die("Sorry Connection failed: " . mysqli_connect_error());
}

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

if (mysqli_query($db_connect, $query_s)) {
  echo "Record deleted successfully";
} else {
  echo "Error deleting record: " . mysqli_error($db_connect);
}

mysqli_close($db_connect);
?>

Example (PDO)

<?php
$hosting_name = "localhost";
$username = "main_user_version";
$password = "password";
$dbname = "myDBPDO";

try {
  $db_connect = new PDO("mysql:host=$hosting_name;dbname=$dbname", $username, $password);
  // set the PDO error mode to exception
  $db_connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

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

  // use exec() because no results are returned
  $db_connect->exec($query_s);
  echo "Record deleted successfully";
} catch(PDOException $e) {
  echo $query_s . "<br>" . $e->getMessage();
}

$db_connect = null;
?>

Example – With One condition

DELETE FROM players
WHERE player_name = 'ViratKohali';

SELECT count(*)
FROM players
WHERE player_name = 'ViratKohali';

Example – With Two conditions

DELETE FROM players
WHERE player_name = 'ViratKohali'
AND player_id < 1000;

SELECT count(*)
FROM players
WHERE player_name = 'ViratKohali'
AND player_id < 1000;

Example – With LIMIT modifier

DELETE FROM players
WHERE player_name = 'ViratKohali'
ORDER BY player_id DESC
LIMIT 1;

DELETE FROM players
WHERE player_name = 'ViratKohali'
ORDER BY player_id ASC
LIMIT 1;

DELETE FROM players
ORDER BY player_id DESC
LIMIT 1;

Example – Using EXISTS Condition

DELETE FROM teams
WHERE EXISTS
  ( SELECT *
    FROM players
    WHERE players.player_id = teams.team_id
    AND player_id > 500 );

SELECT COUNT(*) FROM teams
WHERE EXISTS
  ( SELECT *
    FROM players
    WHERE players.player_id = teams.team_id
    AND player_id > 500 );

I hope you get an idea about Create and delete using MySQLi database.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Read Also:  sql left outer join

About Pakainfo

I am Jaydeep Gondaliya, a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Pakainfo →

Leave a Reply

Your email address will not be published. Required fields are marked *