How can the CONCAT function in MySQL be utilized in PHP to combine two separate columns in a database query?

To combine two separate columns in a MySQL database query using PHP, you can utilize the CONCAT function in your SQL query. This function allows you to concatenate the values of two or more columns into a single column in the result set. By incorporating the CONCAT function in your SQL query within your PHP code, you can easily combine the values from different columns into one.

<?php
// Establish a connection 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);
}

// SQL query with CONCAT function to combine two columns
$sql = "SELECT CONCAT(column1, ' ', column2) AS combined_column FROM table_name";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // Output data of each row
    while($row = $result->fetch_assoc()) {
        echo "Combined Column: " . $row["combined_column"]. "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();
?>