MySQL concat() function
Description
MySQL CONCAT() function is used to add two or more strings. It will return non-binary strings when all the strings are non-binary and return binary string when any of the specified string is binary. A numeric argument is also converted in its equivalent binary string form. It can be avoided by using an explicit type CAST.
Syntax
Arguments
| Name | Description |
|---|---|
| string1 | First string to be joined. |
| string2 | Second string to be joined. Up to N number of strings can be specified this way. |
Example of MySQL concat() function
Code
Explanation
The above MySQL statement will add two strings given in the argument and returns "Use MySQL" string.
Output
.gif)
Example of MySQL concat() function on columns
Sample table : publisher
Code
FROM publisher;
Explanation
The above MySQL statement will add values of pub_city column with values of country column of publisher table placing a '-->' between them.
Output
-example.gif)
PHP script
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>example-concat-function - php mysql examples | w3resource</title>
</head>
<body>
<?php
echo "<h2>list of publisher's city and country with 'publisher's city--->country' format : </h2>";
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='250' align='center'>Publisher's city</td>";
echo "</tr>";
include("../dbopen.php");
$result = mysql_query('SELECT CONCAT(pub_city,"--> ",country)
FROM publisher');
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td align='center' width='200'>" . $row['CONCAT(pub_city,"--> ",country)'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
Please Google+, Like this tutorial on FaceBook, Tweet, save it as bookmark and subscribe with our Feed. Have suggestions? comment using Disqus down this page. Thanks.




