How to replace NULL with zero?

Discussion in 'MySQL' started by obenix, May 15, 2006.

  1. #1
    mysql_query("SELECT sum(qty) FROM table WHERE lid='" . $row['lid'] . "' AND transtype_id='ALL'");
    Code (php):
    Should there be no records tagged with 'ALL', mysql returns a NULL. Instead of NULL, how do I make it return me a ZERO as in 0.

    Thanks.
     
    obenix, May 15, 2006 IP
  2. donteatchicken

    donteatchicken Well-Known Member

    Messages:
    432
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    118
    #2
    $var = mysql_query.........

    if $var = null

    $var = '0';

    .....

    ??
     
    donteatchicken, May 15, 2006 IP
  3. DomainMaster

    DomainMaster Banned

    Messages:
    576
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #3
    what difference does it make? Do this instead:
    $sql=@mysql_query("SELECT sum(qty) FROM table WHERE lid='" . $row['lid'] . "' AND transtype_id='ALL'"); 
    if (!$sql) exit;
    
    Code (markup):
     
    DomainMaster, May 15, 2006 IP
  4. obenix

    obenix Eats an apple a day......

    Messages:
    2,236
    Likes Received:
    180
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Sorry.. I should have been more specific. I'm using the mysql_query command with Maani's PHP Chart.
    http://www.maani.us/charts/index.php?menu=Tutorial&submenu=Chart_Data

    $chart [ 'chart_data' ][ $row1 ][ $count ]=mysql_query("SELECT sum(qty) FROM table WHERE lid='" . $row['lid'] . "' AND transtype_id='ALL'");
    Code (php):
    If the query returns a NULL, the chart will be distorted.
     
    obenix, May 15, 2006 IP
  5. swmcdonnell

    swmcdonnell Active Member

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    66
    #5
    UPDATE table SET field=0 WHERE field IS NULL;
     
    swmcdonnell, Apr 3, 2009 IP
  6. fightrsi

    fightrsi Guest

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Returns 0 if sum(qty) is null
    mysql_query("SELECT COALESCE(sum(qty),0) FROM table WHERE lid='" . $row['lid'] . "' AND transtype_id='ALL'");
     
    fightrsi, Apr 3, 2009 IP