How to Concat First Name and Last Name in PHP MySQL?

Feb 05, 2022 . Admin

Hello Friends,

This article will give you example of how to concat first name and last name in PHP?. I explained simply about concat firstname and lastname in PHP. This tutorial will give you simple concat first name and last name in PHP.

In this post, You'll learn PHP query to concat first name and last name. I will show you use of concat first and last name PHP with space.

Here i will give you many example first name and last name concatenate in PHP.

Table: users


So, let's see bellow solution:

index.php
<?php

$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "db_php";

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

$sql = "SELECT 
            id, CONCAT(first_name, ' ',last_name) as full_name, email, created_at 
        FROM `users`";

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

while ($row = $result->fetch_assoc()) {
    echo '<pre>';
    print_r($row);
}

?>
Output:

Array
(
    [id] => 1
    [full_name] => Hardik Savani
    [email] => savanihd@gmail.com
    [created_at] => 2020-09-12 06:46:08.000000
)
Array
(
    [id] => 2
    [full_name] => Aatman Infotech
    [email] => aatmaninfotech@gmail.com
    [created_at] => 2020-09-30 13:33:52.000000
)
Array
(
    [id] => 3
    [full_name] => Harshad Pathak
    [email] => savanhd2@gmail.com
    [created_at] => 2020-09-12 06:46:08.000000
)
It will help you...
#PHP