MySQL
  Home arrow MySQL arrow Page 4 - Null and Empty Strings
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MYSQL

Null and Empty Strings
By: Shikhar Kumar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 11
    2008-12-02


    Table of Contents:
  • Null and Empty Strings
  • Making Null Safe in PHP
  • Null and MySQL
  • Handling Null in MySQL

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Null and Empty Strings - Handling Null in MySQL
    ( Page 4 of 4 )

    We will enter a null value to make things more clear. The query for that would be as follows:

    // before inserting a value alter the column to accept null values

    ALTER TABLE `example` CHANGE `name` `name` VARCHAR( 20 ) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL


    $query = "INSERT INTO `example` (`id`, `name` ) values ('', null) " ;



    mysql> select * from example;

    +----+------+

    | id | name |

    +----+------+

    | 2 | |

    | 3 | |

    | 4 | NULL |

    +----+------+

    3 rows in set (0.00 sec)


    Now, suppose, you want to select all of the tuples which have null values. The query for this would be as follows:


    Select * from example where name is null ;


    This will return id = 4


    mysql> Select * from example where name is null ;

    +----+------+

    | id | name |

    +----+------+

    | 4 | NULL |

    +----+------+

    1 row in set (0.03 sec)


    Select * from example where name = '' ;


    This will return id = 1,2


    mysql> Select * from example where name = '' ;

    +----+------+

    | id | name |

    +----+------+

    | 2 | |

    | 3 | |

    +----+------+

    2 rows in set (0.00 sec)


    In any practical case, you would want to get all three values because they are all empty. For this you will have to use the following query:


    Select * from example where name is null OR name = '';

    mysql> Select * from example where name is null OR name = '';

    +----+------+

    | id | name |

    +----+------+

    | 2 | |

    | 3 | |

    | 4 | NULL |

    +----+------+

    3 rows in set (0.00 sec)


    And this code becomes a safe way for SQL to handle results when you are not sure what you are handling, null or empty string.

    Before I finish, there's a "gotcha" I would like to mention. Let's insert one more row with id as NULL:


    mysql> INSERT INTO `example` (`id`, `name` ) values ('', 'finalrow');

    Query OK, 1 row affected, 1 warning (0.00 sec)


    mysql> select * from example where id IS NULL;

    +----+----------+

    | id | name |

    +----+----------+

    | 5 | finalrow |

    +----+----------+

    1 row in set (0.00 sec)


    Now, this looks weird but there's an explanation for it. For the benefit of some ODBC applications (at least Delphi and Access), the following query can be used to find a newly inserted row:


    SELECT * FROM tbl_name WHERE auto IS NULL;

    --http://dev.mysql.com/doc/mysql/en/ODBC_and_last_insert_id.html


    All further executions of the same statement provide the expected result:


    mysql> select * from example where id IS NULL;

    Empty set (0.00 sec)

    I hope you now understand the difference between null and an empty string, and how to safely handle them in PHP and MySQL. Good luck!



     
     
    >>> More MySQL Articles          >>> More By Shikhar Kumar
     

       

    MYSQL ARTICLES

    - MySQL Security Tips
    - Designing a MySQL Database: Tips and Techniq...
    - The Three Most Important MySQL Queries
    - Null and Empty Strings
    - MySQL Server Tuning Tips and Tricks
    - MySQL Query Optimizations and Schema Design
    - MySQL Benchmarking Tools and Utilities
    - MySQL Benchmarking Concepts and Strategies
    - Take Some Load off MySQL with MemCached
    - MySQL Table Prefix Changer Tool in PHP
    - Using the SIGNAL Statement for Error Handling
    - Error Handling Examples
    - Error Handling
    - Completing a Search Engine with MySQL and PH...
    - Paginating Result Sets for a Search Engine B...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT