Home Tutorial Php Sql Php Sql Null

 
 

Share on Google+Share on Google+
Php Sql Null
Posted on: December 10, 2010 at 12:00 AM
Advertisement
This example illustrates to describe about null value of execution of sql query in php. 

Php Sql Null

This example illustrates to describe about null value of execution of sql query in php. 

In this example we select data from database table which is not null. We have seen SQL SELECT command along with WHERE clause to fetch data from MySQL table. But when we try to give a condition which compare field or column value to NULL it does not work properly.

To handle this type of problem  MySQL provides three operators:

  • IS NULL: operator returns true of column value is NULL.

  • IS NOT NULL: operator returns true of column value is not NULL.

  • <=> operator compare values, which (unlike the = operator) is true even for two NULL values

 

 Table: tutorial

 

Source Code of sql_null.php 

<?php
  $dbhost = "localhost";
  $dbuser = "root";
  $dbpass = "root";
  $conn = mysql_connect($dbhost, $dbuser, $dbpass);
  if(! $conn ){
    die("Could not connect: " . mysql_error());
  }
  
  if(isset($t_count)){
    $sql = 'SELECT t_author, t_count FROM  tutorial WHERE t_count IS NULL';
  else {
    $sql = 'SELECT t_author, t_count FROM  tutorial WHERE t_count IS NOT NULL';
  }

  mysql_select_db("test");  
  $result = mysql_query$sql, $conn );
  if(! $result ){
    die("Could not get data: " . mysql_error());
  }
  while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
    echo "Author: ".$row['t_author']."<br>""Count: ".$row['t_count']."<br>""<br>";
  }
  echo "Fetched data successfully\n";
  mysql_close($conn);
?>

Download Source Code

 

Output:

Advertisement

Related Tags for Php Sql Null:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 10, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com