How can the "CREATE TABLE IF NOT EXISTS" syntax be used in PHP to create a temporary table in MSSQL?

To create a temporary table in MSSQL using PHP, you can use the "CREATE TABLE IF NOT EXISTS" syntax. This syntax will check if the table already exists before attempting to create it, preventing any errors or conflicts. By using this approach, you can ensure that the temporary table is only created if it does not already exist in the database.

<?php
$servername = "your_servername";
$username = "your_username";
$password = "your_password";
$dbname = "your_dbname";

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

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

// SQL query to create a temporary table if it does not exist
$sql = "CREATE TABLE IF NOT EXISTS #temp_table (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(50) NOT NULL
)";

if ($conn->query($sql) === TRUE) {
    echo "Temporary table created successfully";
} else {
    echo "Error creating temporary table: " . $conn->error;
}

// Close connection
$conn->close();
?>