MySQL Convert in PHP

In this article I explain MySQL convert function in PHP.
  • 1862

CONVERT Function

The MySQL convert function is used to mutate a value of one type (data type) to another type. In other words, the CAST() function takes a value of one type and produces a value of another type. Basically the cost and convert functions do the same thing but there is a little difference between the cast and convert functions.


convert (exp,Type)

The "Type" can have the following values:

  • Binary
  • Char
  • Date
  • DateTime
  • Decimal
  • Signed [integer]
  • Time
  • Unsigned [integer]

Example of CONVERT Function with PHP



if (!$con)


  die('Could not connect: ' . mysql_error());


mysql_select_db("mysql", $con);

  print "<h2>MySQL: Convert Function</h2>";

$result = mysql_query("select CAST(emp_JoinDate as Date) As JoinDate from mcnemployee;");

echo "<table border='1'>




while($row = mysql_fetch_array($result))


  echo "<td>" . $row['JoinDate'] . "</td>";

  echo "</tr>";


  echo "</table>";




© 2020 DotNetHeaven. All rights reserved.