Jump to content

checking to see if value exists in variable


galvin

Recommended Posts

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!

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"

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.