#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Posts
    23
    Rep Power
    0
    hello, all- I could use some help with a small thing that is really causing a terrible time.

    I have a column in a database that is titled 'dirname'.This is the name of the directory that I store images in. Now, since not all of my db records have images associated with them, many times, this field is left blank.

    After a select statement, I need to use an if/else statement to do different things depending upon whether or not this 'dirname' field is populated or not. If it's empty, cool- echo "empty";. If it's populated, echo the data.

    I will be passing the record id to the query such as:
    $result = mysql_query("SELECT dirname FROM a_table WHERE (id='$id')",$db);

    SO I guess I really need help with figuring out how to tell if the dirname field has data in it or not...
    Any help with this would be appreciated. Thanks for your time.

    Rainmaker.


    [This message has been edited by rainmaker (edited September 12, 2000).]
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Posts
    63
    Rep Power
    14
    You could try:

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>$result = mysql_query("SELECT * FROM a_table WHERE (id='$id')",$db);

    if($result_array = mysql_fetch_array($result)) {
    if($result_array["dirname"]) {
    echo($result_array["dirname"]);
    } else {
    echo("empty");
    }
    }
    [/code]

    [This message has been edited by chris22 (edited September 12, 2000).]
  4. #3
  5. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Brussels, Belgium
    Posts
    14,642
    Rep Power
    4476
    The best way to fix this is to actually modify your database and give the dirname column a DEFAULT value of 'empty'

    The following command should do it:

    alter table a_table change column dirname dirname varchar(50) default 'empty';

    With that solution, you wouldn't need the if() statement. If you still want to use the if() then try to use strlen(). Once you pull out dirname and assign it to a variable,

    if (strlen($dirname) == 0) { $dirname="empty"; }

    Hope that helps...

    ---John Holmes...
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Posts
    63
    Rep Power
    14
    Since the data stored is a location of an image, I would assume it is being used within <IMG> tags to display the image. If this is that case and there is an image that has "empty" for it's location, you would get <IMG SRC="empty">. The best way is to have the default in the db to be 0 and use the script I posted above so that you can use the <IMG> tag is there is a value and display something else if the field is empty.
  8. #5
  9. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Brussels, Belgium
    Posts
    14,642
    Rep Power
    4476
    yeah, for an <IMG> tag, your method would work better.

    the question says he just wants to echo 'empty' or echo the data, if it exists. having a default value would be ideal in that case, since there are no checks, you just echo what you pulled out.

    it all depends on your situation..

    ---JH

Similar Threads

  1. Requesting help on READTEXT statement
    By hdokes in forum MS SQL Development
    Replies: 1
    Last Post: March 19th, 2004, 08:53 AM
  2. Question about the WHERE statement
    By layz d in forum MS SQL Development
    Replies: 5
    Last Post: January 28th, 2004, 11:58 AM
  3. how can i put an IF statement inside a WHILE statement?
    By Jarrett Green in forum Perl Programming
    Replies: 4
    Last Post: December 22nd, 2003, 10:05 PM
  4. Update database/count statement
    By Signuml99 in forum Database Management
    Replies: 2
    Last Post: November 26th, 2003, 01:07 PM
  5. Figuring out parts of a mySQL statement
    By ChibiGuy in forum PHP Development
    Replies: 2
    Last Post: August 18th, 2003, 10:38 PM

IMN logo majestic logo threadwatch logo seochat tools logo