Results 1 to 8 of 8

Thread: ifnull issue

  1. #1
    Join Date
    Sep 2010
    Posts
    27

    Unanswered: ifnull issue

    I am using the following line in a PHP file:
    SELECT * FROM `poems` WHERE `poemID`=1

    now i want the following in addition with the above line:
    if PoetName is NULL then PoetName is "Unknown"

    but i am unable to solve this issue with ifnull() function. please somebody solve this problem.

    I am using mysql

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Code:
    SELECT something
         , anything
         , just_not_the_dreaded_evil_select_star 
         , COALESCE(PoetName,'Unknown') AS PoetName
      FROM poems 
     WHERE poemID = 1
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Sep 2010
    Posts
    27
    Ok i tried but it didn't work, although it didn't result in an error either. Poet field remained empty in output page.
    mysql_query(
    "SELECT ghazalID, NameUrdu, TextUrdu, PoetUrdu, NameRoman, TextRoman, PoetRoman, COALESCE(PoetRoman,'Unknown') FROM ghazal_list WHERE ghazalID='" . $_GET[ghazalID] ."'");

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by belask View Post
    Ok Poet field remained empty in output page.
    you forgot something

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  5. #5
    Join Date
    Sep 2010
    Posts
    27
    That's for sure, but what? Do you have any idea?
    As far as i know, i edited the correct php file.
    There is no syntax error, because the page is showing correctly. (although there were some errors during the editing, but i fixed them)
    I am sure that particular field is Null in the database table.
    http://img211.imageshack.us/img211/7194/nullfields.png
    Last edited by belask; 02-09-11 at 19:59.

  6. #6
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    run your query outside of php, i.e. directly in mysql

    you will see that it actually does work

    then inspect your php code for the source of the error
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  7. #7
    Join Date
    Sep 2010
    Posts
    27
    Quote Originally Posted by r937 View Post
    run your query outside of php, i.e. directly in mysql

    you will see that it actually does work

    then inspect your php code for the source of the error
    Thanks
    Only after runing the query in mysql i could figure out the problem.
    The problem was that I didn't blindly follow your syntax in the first place and didn't include AS PoetName.
    Working perfectly now
    And thanks for not telling me my mistake too early. Your strategy made me understand the meaning of AS PoetName.
    Last edited by belask; 02-09-11 at 22:08.

  8. #8
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by belask View Post
    And thanks for not telling me my mistake too early. Your strategy made me understand the meaning of AS PoetName.
    you're welcome

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

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