Using the Sql ALTER TABLE statement, it is possible to add a new column to the MySQL table in PHP.
syntax:
ALTER TABLE 'table_name' add 'new_column_name' [type];
You can also specify where you want to add the field.
The FIRST clause add a new column at first place within a table.
The AFTER clause where to place the new column in relation to existing columns.
<?php
$dbhost = "localhost";
$dbuser = "your_username";
$dbpass = "your_password";
$dbname = "basename";
$conn = mysqli_connect($dbhost, $dbuser, $dbpass , $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "ALTER TABLE membership
ADD `date` DATETIME DEFAULT CURRENT_TIMESTAMP
AFTER surname";
if (mysqli_query($conn, $sql)) {
echo "The column has been added.";
} else {
echo "The column has not been added." . mysqli_error($conn);
}
mysqli_close($conn);
?>