Use MySQL IFNULL In PHP

In this article I explain MySQL ifnull function in PHP
  • 2743

IFNULL Function

The MySQL IFNULL function takes two expressions and if the first expression is not null then it returns the first expression, otherwise it returns the second expression. In other words, the MySQL IFNULL function lets you replace a non-null value with a null value.


Syntax

IFNULL (test_expression, replacement_value );


Parameters in IFNULL function

It takes three parameters; they are:
 

Parameter Description
test_expression It specifies a test expression.
replacement_value Specifies replacement value.

Example of MySQL Ifnull function in PHP

<?php

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

if (!$con)

  {

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

  }

mysql_select_db("mysql", $con);

print "<h2>MySQL: Use of IF function</h2>";

$result = mysql_query("select payment_date,IFNULL(payment_date,'NewDate') as NewDate from vendordtl");

 

echo "<table border='1'>

<tr>

<th>PaymentDatee</th>

<th>NewDate</th>

</tr>";

while($row = mysql_fetch_array($result))

  {

  echo "<tr>";

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

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

  echo "</tr>";

  }

  echo "</table>";

?>

Output

mysql-ifnull-function-in-php.jpg

© 2019 DotNetHeaven. All rights reserved.