ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

SQL NULL Values

sqltuts

When SELECT command is used along with WHERE clause  in order to fetch data from MySQL table of a given database, but the condition implied doesn’t have any result which means it’s empty .

Like you want to search a person whose age is 21 but when you search through the database there is no person like that so it shall return NULL Value but it doesn’t show.

So To handle such those conditions MySQL provides three unique commands

  • IS NULL:command returns true if the needed column value of a particular table is NULL.
  • IS NOT NULL:command returns true if the needed column value of a particular table is not NULL..
  • <=>:command compares values, which (unlike the = operator) is true even for two NULL values.

To look for columns that are or are not NULL, use IS NULL or IS NOT NULL.

USE OF NULL Values in PHP Script:

 

You can use if…else or any other condition in order to get a query based on NULL value.

 

Example:

 

  1. <?php
  2. $dbhost = 'localhost';
  3. $dbusername = 'root';
  4. $dbpassword = 'rootpassword';
  5. $conn = mysql_connect($dbhost, $dbusername, $dbpassword);
  6. if(! $con )
  7. {
  8.   die('Could not connect: ' . mysql_error());
  9. }
  10. if( isset($tutorial_ID ))
  11. {
  12.    $sql = 'SELECT tutorial_Fname, tutorial_Lname
  13.           FROM  tcount_person
  14.           WHERE tutorial_Fname = $tutorial_Lname';
  15. }
  16. else
  17. {
  18.    $sql = 'SELECT tutorial_ Fname, tutorial_ Lname'
  19.            FROM  tcount_person
  20.            WHERE tutorial_ Fname IS $tutorial_ Fname;
  21. }
  22.  
  23. mysql_select_db('Database TUTORIALS');
  24. $retval = mysql_query( $sql, $con );
  25. if(! $result )
  26. {
  27.   die('Could not get data: ' . mysql_error());
  28. }
  29. while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
  30. {
  31.     echo " Fname:{$row['tutorial_ Fname]}  <br> ".
  32.          " Lname: {$row['tutorial_ Lname]} <br> ".
  33.          "--------------------------------<br>";
  34. }
  35. echo " data  has been successfully fetched \n";
  36. mysql_close($con);
  37. ?>
Facebook Comments

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!