contra10 Posted September 10, 2009 Share Posted September 10, 2009 I'm trying to echo out a statement if somethin gis untrue but its not working <?php $queryear = "SELECT * FROM `ratings` WHERE `songtitle` = '$name'"; $resultear = mysql_query($queryear) or die(mysql_error()); while ($postedear = mysql_fetch_assoc($resultear)) { $avsong = "{$postedear['songtitle']}"; $linksong = "{$postedear['song']}"; $ratedavg = "{$postedear['average']}"; if ($ratedavg > 1){ echo"<td>$ratedavg / 10</td></tr>"; }else{ echo "<td>No Rating</td></tr>"; } } ?> THe average shows for those that have been rated but the other statement is suppose to show for those that havn't but it doesn't[/code] Quote Link to comment https://forums.phpfreaks.com/topic/173818-cant-echo-out-else-statement/ Share on other sites More sharing options...
jcombs_31 Posted September 10, 2009 Share Posted September 10, 2009 Have you check the value of $ratedavg to debug your code. For one you are setting it to a string and then checking against an int. Quote Link to comment https://forums.phpfreaks.com/topic/173818-cant-echo-out-else-statement/#findComment-916211 Share on other sites More sharing options...
contra10 Posted September 10, 2009 Author Share Posted September 10, 2009 i see what your saying in this case one of my $ratedavg = 5 5 > 1 so it would show the result but if there is no rating and the $ratedavg is equal to nothing shouldn't it show. Or would I have to put something like <?php if ($ratedavg == true){ echo"<td>$ratedavg / 10</td></tr>"; }else{ echo "<td>No Rating</td></tr>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/173818-cant-echo-out-else-statement/#findComment-916223 Share on other sites More sharing options...
jcombs_31 Posted September 10, 2009 Share Posted September 10, 2009 Are you allowing null values in your database? What I was saying is that "5" != 5, but I think PHP may be forgiving and cast it as an int for you when doing the comparison. Quote Link to comment https://forums.phpfreaks.com/topic/173818-cant-echo-out-else-statement/#findComment-916228 Share on other sites More sharing options...
contra10 Posted September 10, 2009 Author Share Posted September 10, 2009 in my databse the "average" is set to "not null" Quote Link to comment https://forums.phpfreaks.com/topic/173818-cant-echo-out-else-statement/#findComment-916229 Share on other sites More sharing options...
mikesta707 Posted September 10, 2009 Share Posted September 10, 2009 I think it would cast the string as an int in that comparison, and all strings get cast to 0 if i'm not mistaken. So do this $ratedavg = $postedear['average']; and see if it helps. There is no reason to surround the data with quotes anyways, as if its a string it will be treated as such when assigned, and if its an int it will be treated as such, but forcing it to be a string always can run into problems Quote Link to comment https://forums.phpfreaks.com/topic/173818-cant-echo-out-else-statement/#findComment-916253 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.