SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot Para's Avatar
    Join Date
    Mar 2005
    Location
    London UK
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    if statement, how to reconise the DB field is NULL ?

    Hi i need help with this if statement, i want it to reconise if a field in my table is empty, if it is i want it to echo 'NO CODE 2' and if it has data i want it to echo '<textarea>$codes2</textarea>'

    At the moment, its only doing the else echo statement on both empty and non empty fields? Can anyone point me in the right direction?

    Thanks in advance for any help.

    PHP Code:
    <?php
    $sql 
    "SELECT * FROM codes_table WHERE id=".$_GET['id'];
    $rs $db->Execute$sql );
    $codes2 $rs->fields['codes2'];

    if (
    $codes2 === 'NULL'){
    echo 
    "NO CODE 2";
    } else {
        echo 
    "<textarea>$codes2</textarea>";
    }
    ?>

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,347
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    well, i don't really do php but your code will be a lot cleaner if you substitute the 'NO CODE 2' string right in the SQL

    you should also get out of the habit of using the dreaded, evil "select star"
    Code:
    select id
         , foo
         , coalesce(codes2,'NO CODE 2') as codes2
         , bar
      from codes_table 
     where id = ".$_GET['id']
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if ($codes2 === NULL) { 
    Or
    PHP Code:
    if (is_null($codes2)) { 

  4. #4
    SitePoint Zealot Para's Avatar
    Join Date
    Mar 2005
    Location
    London UK
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks kyberfabrikken, works perfect!!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •