What potential issues can arise when using mysql_query() to fetch column names in PHP?

When using mysql_query() to fetch column names in PHP, potential issues can arise due to the deprecated nature of the function and its vulnerability to SQL injection attacks. To solve this, it is recommended to use mysqli or PDO extension for database interactions, as they provide prepared statements to prevent SQL injection and are more secure.

// Using mysqli extension to fetch column names securely
$conn = new mysqli($servername, $username, $password, $dbname);

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

$sql = "SELECT * FROM your_table_name LIMIT 1";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    $columns = $result->fetch_fields();
    
    foreach ($columns as $column) {
        echo $column->name . "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();