galvin Posted October 17, 2011 Share Posted October 17, 2011 If I do a mysql query from a database on a field that either has a number or is blank, how can I write php to check that. In other words, I am getting that value back into a variable called $isaresult and doing... if ($isaresult !== '') { //value exists } else { //no value yet } It's that first line of code that doesn't seem to be working. Should I be using "null" or something instead? I'm not real experienced with checking if a value exists, so let me know the proper way to do it (since I assume there are multiple ways). Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/249257-checking-to-see-if-value-exists-in-variable/ Share on other sites More sharing options...
ZulfadlyAshBurn Posted October 17, 2011 Share Posted October 17, 2011 post your code. Quote Link to comment https://forums.phpfreaks.com/topic/249257-checking-to-see-if-value-exists-in-variable/#findComment-1279921 Share on other sites More sharing options...
galvin Posted October 17, 2011 Author Share Posted October 17, 2011 I think this below is all that is necessary (this is all within a larger loop, but it's all working right...my only issue is figuring how to properly say in plain english "If $isaresult has nothing in it, do this. But if $isaresult has any type of value in it (including 0), do this". FYI, the "result" column in the database is blank except for one of them that has the number "1" in it (for testing purposes)... $sql = "SELECT result FROM picks WHERE picks.userid = '{$users["userid"]}' AND picks.teamid = '{$nextpick["teamid"]}' AND picktype='high'"; $istherearesult= mysql_query($sql, $connection); if (!$istherearesult) { die("Database query failed: " . mysql_error()); } else { $isaresult = mysql_fetch_array($istherearesult); if ($isaresult !== '') { $class="done"; } else { $class="notyet"; } } When i do that, the class variable is showing as "done" for all of them, even though I put the number "1" in one of them. That one should be coming up with a class of "notyet" Quote Link to comment https://forums.phpfreaks.com/topic/249257-checking-to-see-if-value-exists-in-variable/#findComment-1279924 Share on other sites More sharing options...
ZulfadlyAshBurn Posted October 17, 2011 Share Posted October 17, 2011 should use $isaresult = mysql_fetch_array($istherearesult); if (!empty($isresult)) { $class="done"; } else { $class="notyet"; } Quote Link to comment https://forums.phpfreaks.com/topic/249257-checking-to-see-if-value-exists-in-variable/#findComment-1279925 Share on other sites More sharing options...
galvin Posted October 17, 2011 Author Share Posted October 17, 2011 Wait, I think I just realized my stupid mistake... Quote Link to comment https://forums.phpfreaks.com/topic/249257-checking-to-see-if-value-exists-in-variable/#findComment-1279926 Share on other sites More sharing options...
PFMaBiSmAd Posted October 17, 2011 Share Posted October 17, 2011 $isaresult is either a FALSE value (if there was no row for the mysql_fetch_xxxxx statement to fetch) or an array. You would need to test a specific element of the $isaresult array. Quote Link to comment https://forums.phpfreaks.com/topic/249257-checking-to-see-if-value-exists-in-variable/#findComment-1279927 Share on other sites More sharing options...
galvin Posted October 17, 2011 Author Share Posted October 17, 2011 Ok all set now. Totally forgot to specify the mysql field (called "result"), i.e. instead of if(!empty($isaresult)) , it needed to be if (!empty($isaresult['result'])). Sorry for wasting your time. Although, thank you for telling me about using "!empty" to check. I will use that syntax going forward Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/249257-checking-to-see-if-value-exists-in-variable/#findComment-1279929 Share on other sites More sharing options...
PFMaBiSmAd Posted October 17, 2011 Share Posted October 17, 2011 A numerical zero value is considered, by php and the empty() statement, to be empty. Quote Link to comment https://forums.phpfreaks.com/topic/249257-checking-to-see-if-value-exists-in-variable/#findComment-1279938 Share on other sites More sharing options...
galvin Posted October 17, 2011 Author Share Posted October 17, 2011 Interesting. So since there sometimes will be zero values that I do NOT want to be seen as empty, I guess I can't use the empty syntax in this case. I'll try !=="" instead... Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/249257-checking-to-see-if-value-exists-in-variable/#findComment-1279940 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.