How can the error "Table 'kante' already exists" be avoided when creating a table in PHP?

When creating a table in PHP, the error "Table 'kante' already exists" can be avoided by checking if the table already exists before attempting to create it. This can be done by querying the database schema to see if the table name is already in use. If the table does not exist, then the table creation query can be executed.

<?php

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

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

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Check if table 'kante' already exists
$table_name = 'kante';
$result = $conn->query("SHOW TABLES LIKE '$table_name'");

if($result->num_rows == 0) {
    // Table does not exist, create it
    $sql = "CREATE TABLE $table_name (
        id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
        name VARCHAR(30) NOT NULL
    )";

    if ($conn->query($sql) === TRUE) {
        echo "Table '$table_name' created successfully";
    } else {
        echo "Error creating table: " . $conn->error;
    }
} else {
    echo "Table '$table_name' already exists";
}

$conn->close();

?>