Similarly as you supplement records into tables, you can erase records from a table utilizing the SQL DELETEstatement. It is regularly utilized in conjugation with the WHERE provision to erase just those records that matches explicit criteria or condition.
The essential grammar of the DELETE proclamation can be given with:
Erase FROM table_name WHERE column_name=some_value
We should make a SQL question utilizing the DELETE proclamation and WHERE provision, after that we will execute this inquiry through passing it to the PHP mysqli_query() capacity to erase the tables records. Think about the accompanying people table inside the demo database:
The PHP code in the accompanying model will erase the records of those people from the people table whose first_name is equivalent to John.
$mysqli = new mysqli("localhost", "root", "", "Mydb"); if( $mysqli === false ){ die("ERROR: Could not connect. " . $mysqli->connect_error); } $sql = "DELETE FROM Data WHERE ID=201"; if($mysqli->query($sql) === true){ echo "Record was deleted successfully."; } else{ echo "ERROR: Could not able to execute $sql. " . $mysqli->error; } $mysqli->close();
try{ $pdo = new PDO("mysql:host=localhost; dbname=Mydb", "root", ""); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch(PDOException $e){ die("ERROR: Could not connect. " . $e->getMessage()); } try{ $sql = "DELETE FROM Data WHERE ID=201"; $pdo->exec($sql); echo "Record was deleted successfully."; } catch(PDOException $e){ die("ERROR: Could not able to execute $sql. " . $e->getMessage()); } unset($pdo);
Very affordable projects!! And that to submit before deadlines. Thanks for helping me in my database project and raising my grades. I have been able to secure good marks in my internal assessment only because of you. Read More
Follow Us