Using an if statement to check for blank database entries

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
wmowat
Forum Newbie
Posts: 6
Joined: Sun Sep 21, 2003 3:29 pm
Location: North Bay, ON, CA

Using an if statement to check for blank database entries

Post by wmowat »

Hello,

I have got my page working for the most part now, but I'm trying to add an if statement.

What I want it to do is check to see if there is anything in the field, if there isn't I want it to print "Not Rated". This is the code I have so far.

MRating and WRating are the names of a field in a database, they should have a rating out of 10. eg. 2/10 4/10 etc. but if they are empty, I want the words "Not Rated" to be put in their place. Can anyone tell me what is wrong with this code?

Code: Select all

$Mrating = $lineї'MRating'];
$Wrating = $lineї'WRating'];
	   			
if (empty($Mrating)) 
{then $Mrating = "Not Rated";}

if (empty($Wrating)) 
{then $Wrating = "Not Rated";}
Thanks again,

Will Mowat
wmowat@nearnorthweb.com
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

You could instead do this I think:

Code: Select all

$Mrating = $lineї'MRating'];
$Wrating = $lineї'WRating'];
               
if (empty($Mrating))
{$Mrating = "Not Rated";}

if (empty($Wrating))
{$Wrating = "Not Rated";}
Or even:

Code: Select all

if (empty($lineї'MRating']) {
$Mrating = "Not Rated";
} else {
$Mrating = $lineї'MRating'];
}
wmowat
Forum Newbie
Posts: 6
Joined: Sun Sep 21, 2003 3:29 pm
Location: North Bay, ON, CA

Post by wmowat »

Sill not working, I get a parse error no matter what I try and do. I had it kind of working before, but it would put Not Rated for all of them instead of just the empty ones, also I think my database sets all the ratings fields to "Null" as a default value. I tried plugging that in in place of "" but nothing happened. Any more ideas?
wmowat
Forum Newbie
Posts: 6
Joined: Sun Sep 21, 2003 3:29 pm
Location: North Bay, ON, CA

Post by wmowat »

Never mind, I got it working. Thanks...your code helped me get it going. I did it based on your 2nd example

Code: Select all

if ($lineї'MRating'] == "") { 
$Mrating = "Not Rated"; 
} else { 
$Mrating = $lineї'MRating']; 
}

if ($lineї'WRating'] == "") { 
$Wrating = "Not Rated"; 
} else { 
$Wrating = $lineї'WRating']; 
}
That is the working code
Last edited by wmowat on Fri Sep 26, 2003 6:13 pm, edited 1 time in total.
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

What does the parse error say?

And can you post your loop construct to loop through the query results?
Unipus
Forum Contributor
Posts: 409
Joined: Tue Aug 26, 2003 2:06 pm
Location: Los Angeles, CA

Post by Unipus »

How about:

Code: Select all

$Mrating = $line["MRating"]; 
$Wrating = $line["WRating"]; 
                
if (!$Mrating) 
{$Mrating = "Not Rated";} 

if (!$Wrating) 
{$Wrating = "Not Rated";}
If that doesn't work, what exactly is the error you're getting? Have you tested the output of $Mrating before and after you re-assign it? Have you checked your query to make sure you're getting the right values in the first place?
Post Reply