Uploaded by hosnysys

php [using mysqli to delete record from the MySQL database]

advertisement
<?php
//Initial
$dbhost =
$dbuser =
$dbpass =
$dbname =
the variables
'localhost';
'root';
'root';
'db';
//Connect to the MySQL server and open the database
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname)
or die('Could not connect: ' . mysqli_error($conn));
//Select record from employees table with where clause
$sql_select = 'SELECT * FROM employees WHERE EMPLOYEE_ID = 198';
$result = mysqli_query($conn, $sql_select)
or die('Could not select data: ' . mysqli_error($conn));
//Count the row number
$row_count = mysqli_num_rows($result);
if($row_count == 0)
{
echo 'This employee number does not exist.';
exit;
}
//Delete record from "employees" table
$sql_delete = 'DELETE FROM employees ' .
'WHERE EMPLOYEE_ID = 198';
$result = mysqli_query($conn, $sql_delete)
or die('Could not delete data: ' . mysqli_error($conn));
echo 'Data deleted successfully.';
mysqli_close($conn);
?>
Download