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)

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)



Example (PDO)

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 . "
" . $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.

Leave a Comment