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