SQL NULL Values

0
10

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
(Visited 14 times, 1 visits today)
SHARE
Previous articleSQL Join Command
Next articleSQL Regular Expressions
Hello Itsourcecoders, welcome to itsourcecode.com. I'm Joken Villanueva, MIT a passionate Blogger, Programmer and a Hobbyist. I started Itsourcecode because I wanted to give back and Share all the learnings and knowledge I've learned in my career and I believe through this website I would be able to help and assist those newbie programmers in enhancing their skills from different programming languages. So let us all help each other by sharing our ideas!

LEAVE A REPLY

Please enter your comment!
Please enter your name here