How to check against NULL results in an array...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Software_Pyrate
Forum Newbie
Posts: 10
Joined: Sat May 22, 2010 3:00 am

How to check against NULL results in an array...

Post by Software_Pyrate »

I have read the manual...and I thought this should be working...but its not..please help this poor sap :(

I have a basic register form, and I run a query for the submitted name. It works when a value is found and it echo's out " that name is already taken" ....

But when the query does not find the name...and I believe i'm getting a NULL back out of the query, my if/else statement is not executing....

I'm new and I'm sure my code is a little bloated...but can you guys tell my why the (2nd)if statement is true...is not echoing out the statement....

Code: Select all

function check_if_exists($req_username)
{
		
	if($_POST['username'] !== "" and $_POST['username'] !== NULL)
	{	
		$query = mysql_query("SELECT * FROM login WHERE user_name = '$req_username'");
		while($row = mysql_fetch_array($query))
		{
			if($row['user_name']== NULL or $row['user_name'] == "")
			{
			echo "thAT NAME IS AVAILABLE";	
			}
			else
			{ 
	                echo " that name is already taken";
			}
		}
				
				
	}
else
	{
	echo "Fields cannot be blank";
	}
}
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to check against NULL results in an array...

Post by requinix »

If it doesn't find the name then there won't be any results. You won't get anything back. No rows means mysql_fetch_array returns false on the very first try and the if block doesn't get a chance to execute.

Rearrange the code a bit.
User avatar
Software_Pyrate
Forum Newbie
Posts: 10
Joined: Sat May 22, 2010 3:00 am

Re: How to check against NULL results in an array...

Post by Software_Pyrate »

Well...thanz 4 the help...

Turns out I WAS getting a NULL back out of the array.I tried if == FALSE but it didn't work

I did a simple print($query) and I got a resource #id 5 back...which told me that it was in fact a null or empty array.....

To modify...i just did an if with:

if(mysql_fetch_array($query) == NULL)......0 works too...
then I ran my while statement again with the array if a value is in it...


Thanks man....This forum is the best...
Peace
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to check against NULL results in an array...

Post by requinix »

Software_Pyrate wrote:I did a simple print($query) and I got a resource #id 5 back...which told me that it was in fact a null or empty array.....
That merely means you got a resource - says nothing about how many rows came back from the query.
You use that resource with functions like mysql_fetch_array or mysql_num_rows.
Post Reply