MySQL Delete Query Execute Using PHP
DELETE keyword is used basically to delete 1 or more than one record from the database table. even if we delete each and every row of a table then also the schema of the table remains undeleted. It,s necessary to use where clause in delete query, otherwise all the records will be deleted.
DELETE FROM table_name WHERE some_column = some_value
id | name | salary |
---|---|---|
1 | Name1 | 55000 |
2 | Name2 | 60000 |
3 | Name3 | 70000 |
<?php
$host = "localhost";
$user = "";
$pass = "";
$dbname = "users";
$conn = mysql_connect($host,$user,$pass) or die(mysql_error());
if(!$conn){
die("Could not connect:".mysql_connect_error());
}
// select database
mysql_select_db($dbname,$conn);
$id=2;
$sql = "delete from users where id=".$id;
if(mysql_query($sql)){
echo "Record deleted successfully";
}else{
echo "Could not deleted record: ". mysql_error();
}
?>
Record deleted successfully
id | name | salary |
---|---|---|
1 | Name1 | 55000 |
3 | Name3 | 70000 |
© 2024 Easy To Learning. All Rights Reserved | Design by Easy To Learning