How does the use of PHP's mysql_query function compare to direct MySQL queries for checking table existence?

When checking for table existence in MySQL, using the mysql_query function in PHP is not recommended as it is deprecated and insecure due to SQL injection vulnerabilities. Instead, it is better to use the mysqli or PDO extension in PHP to execute parameterized queries for checking table existence.

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

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

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

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

if ($result->num_rows > 0) {
    echo "Table exists";
} else {
    echo "Table does not exist";
}

$conn->close();
?>