Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 5 of 5
  1. #1
    Senior Member
    Join Date
    Aug 2011
    Posts
    226
    Member #
    29153
    Liked
    4 times

    How do I read a MySQL data with a null field?

    I have
    $pageSource = mysql_query("SELECT * FROM pmkArticles WHERE category = '$directory1'");

    I have a field where category is empty and if $directory1 is empty it does not read that field. Why?

  2.  

  3. #2
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,686
    Member #
    5580
    Liked
    716 times
    The concept of "empty" VS "null" ... can be confusing.

    Your query doesn't get that result because $directory1 = '' (is empty), but 'category' is NULL.
    So they are not equal.

    Empty is a column value that has a value of ''
    The value is there, it's just empty.

    Null is a column value that isn't even there.
    It doesn't know if it's empty or not.

    You should decide whether or not to allow NULL in that column.

    If you use phpMyAdmin and view that column, you can specify NOT NULL for that column, by default.
    When a new row is added, that field will then be 'empty', not 'null'.
    Try that and see if you only need to check for 'empty' and not worry about null.

    To twist your mind some more ...
    PHP variables can also be empty or null.
    In your case $directory1 is empty.
    But you could do this: if( $directory1 == '' ){ $directory1 = NULL); }
    Now, $directory1 is NULL and it would show up in your result only if `column` is NULL.
    Last edited by mlseim; Apr 01st, 2016 at 03:28 PM.


  4. #3
    Senior Member
    Join Date
    Aug 2011
    Posts
    226
    Member #
    29153
    Liked
    4 times
    Thanks. I went ahead and put in a bunch of if then elseif statements. I'll keep in mind what you said for the next time.

  5. #4
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,686
    Member #
    5580
    Liked
    716 times
    You need to start using MySQLi (i=improved) or PDO (prepared statements).

    Most webhosts are now eliminating MySQL (the version many people still use ... is now obsolete in the latest PHP versions).

    Google "convert from mysql to mysqli"


  6. #5
    Junior Member
    Join Date
    Mar 2016
    Location
    Chicago, IL
    Posts
    24
    Member #
    53671
    Liked
    8 times
    A simple change from
    $pageSource = mysql_query("SELECT * FROM pmkArticles WHERE category = '$directory1'");

    to
    $pageSource = mysql_query("SELECT * FROM pmkArticles WHERE category " . ($directory1 == ''' ? "LIKE '' OR category IS NULL" : "LIKE '$directory1'") . ')';

    will deliver the desired result.


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 05:23 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com