Using the Sql ALTER TABLE statement, it is possible to delete the column in the MySQL table using PHP.

syntax:
ALTER TABLE `table_name` DROP `column_name`;

Example:



<?php
$dbhost 
"localhost";
$dbuser "yourusername";
$dbpass "yourpassword";
$dbname "yourdatabase";
$conn mysqli_connect($dbhost$dbuser$dbpass $dbname);
// Check connection
if (!$conn) {
die(
"Connection failed: " mysqli_connect_error());
}
$sql "ALTER TABLE membership
DROP `date`"
;
if (
mysqli_query($conn$sql)) {
echo 
"Column deleted successfully";
} else {
echo 
"Deleting a column failed" mysqli_error($conn);
}
mysqli_close($conn);
 
?>




It is also possible to delete multiple columns in the MySQL table.

syntax:
ALTER TABLE "table_name" DROP "column_name1", DROP "column_name2";

Example:



<?php
$dbhost 
"localhost";
$dbuser "yourusername";
$dbpass "yourpassword";
$dbname "yourdatabase";
$conn mysqli_connect($dbhost$dbuser$dbpass $dbname);
// Check connection
if (!$conn) {
die(
"Connection failed: " mysqli_connect_error());
}
$sql "ALTER TABLE membership
DROP `lastname`, DROP `date`"
;
if (
mysqli_query($conn$sql)) {
echo 
"Columns deleted successfully";
} else {
echo 
"Deleting a columns failed" mysqli_error($conn);
}
mysqli_close($conn);
 
?>










Privacy Policy