MySQL Convert in PHP

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

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.

Syntax

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

<?php

$con=mysql_connect("localhost","root","");

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'>

<tr>

<th>EmpJoinDate</th>

</tr>";

while($row = mysql_fetch_array($result))

  {

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

  echo "</tr>";

  }

  echo "</table>";

?>

Output

mysql-convert-function-in-php.jpg

© 2019 DotNetHeaven. All rights reserved.