What is the potential issue with adding a new column to a table in PHP using ALTER TABLE?

When adding a new column to a table in PHP using ALTER TABLE, the potential issue is that the column may already exist in the table, causing an error. To solve this issue, you can first check if the column exists before attempting to add it.

// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check if the column exists before adding it
$column_name = "new_column";
$sql = "SHOW COLUMNS FROM table_name LIKE '$column_name'";
$result = $conn->query($sql);

if ($result->num_rows == 0) {
    // Add the new column to the table
    $sql = "ALTER TABLE table_name ADD $column_name VARCHAR(255)";
    if ($conn->query($sql) === TRUE) {
        echo "New column added successfully";
    } else {
        echo "Error adding new column: " . $conn->error;
    }
} else {
    echo "Column already exists in the table";
}

// Close the database connection
$conn->close();