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!

Link to comment
Share on other sites

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"

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.