Home Tutorial Php Sql Php Sql Null

 
 

Php Sql Null
Posted on: December 10, 2010 at 12:00 AM
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:

Related Tags for Php Sql Null:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.