PHP SQLite delete table

We will create the database and connection object.
$my_conn = new PDO('sqlite:my_student.sqlite3');
We will use $my_conn to execute our query.

By using drop query to delete table.

Deleting table

Deleting table
$my_conn = new PDO('sqlite:my_student.sqlite3');

// Set errormode to exceptions
$my_conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);

 $sql=$my_conn->prepare("DROP TABLE  student");

if($sql->execute()){
echo " <br><br>Table deleted. ";
}else{
print_r($sql->errorInfo()); 
}
$my_conn = null;
Output is here
Table deleted.
User Confirmation: Suggest adding an additional layer of security by confirming with the user (e.g., through a form or prompt) before executing the delete operation.
$status=$_GET['status']; // Getting value from query string
if($status=='yes'){
try{
$sql=$my_conn->prepare('DROP TABLE student');
if($sql->execute()){
    echo "<br> student table deleted. ";
}
}
catch(PDOException $e){
	echo " Table not deleted : ".$e->getMessage();
}
}else{
    echo "<br><a href=drop.php?status=yes> 
    Clcik here to delete table</a>";
}
We will check the table if exists or not by using sqlite_master before deleting it. The first line is the query for checking sqlite_master with type=table and name=table_name by using WHERE condition.

If the table is available then we will delete it.
$q="SELECT name FROM sqlite_master WHERE type='table' AND name='student'";
if($result=$my_conn->query($q)){
$row = $result->fetch(PDO::FETCH_OBJ);
if($row->name=='student'){
if(strlen($todo)==3 and $todo=='yes'){	
 $sql=$my_conn->prepare("DROP TABLE  student");

if($sql->execute()){
echo " <br><br>Table deleted. <br> ";
}else{
print_r($sql->errorInfo()); 
}
$my_conn = null;
   
} // end of checking todo
}else{ // end of if else if student table exits 
echo " <br><br>student table is not available<br> "; 
}	
}
Delete Records only
Download sample script for SQLite with instructions on how to use.



Delete Records Update records

PHP SQLite PDO Functions
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Post your comments , suggestion , error , requirements etc here





    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer