Page 1 of 1

Will empty or NULL values from database be returned ?

Posted: Sun May 24, 2009 5:19 pm
by smashhell
Well, this is a newbie question that I can't find the answer. :)
If you have empty values or NULL value in your database, will it get return ?

here is a simple database:
id name
1 bob
2
3 NULL
4 chris

here is my simple code:
$query = mysql_query("SELECT * FROM table ORDER BY id ASC", $connection);
while ($array = mysql_fetch_array($query)) {
$name = $array['name'];
}

Now the question is what is inside the $name array ?
Will it be array([0]=>bob [1]=> [2]=> [4]=>chris) ?
or will it be array([0]=>bob [1]=>chris) ?

Thank you all in advance ! :mrgreen:

Re: Will empty or NULL values from database be returned ?

Posted: Mon May 25, 2009 3:21 pm
by Darhazer
The easier way to find the answer is to try to run the code :-)

Yes, empty and NULL values are returned, otherwise what is the outer joins for ?
Actually the problem is in your PHP code, not in the SQL, because you are overwriting the $array variable and in the end it will contain only the last row.

But if you rewrite this as follow:

Code: Select all

while ($row = mysql_fetch_array($query)) {
$array[] = $row['name'];
}
In the end the $array will hold all 4 records

Re: Will empty or NULL values from database be returned ?

Posted: Mon May 25, 2009 11:42 pm
by smashhell
Thank you for the reply !

I couldn't test out the code cause I don't have PHP access at that time. :D

good to know that the empty and NULL values are returned.

but what is it that you say my code is wrong? I don't see a difference. =_=
How did the $array gets overlapped ?

Edit:
Well I just tested it, and your code works and mine doesn't. :(
That however, causes another big problem for me. :?
It wouldn't let me set the name of the $array[] to anything else.

Than what if I have more than one $array[] in the same page, like a function page ?
How do I solve such a problem ?
Thank you again.