Empty field

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
snicolas
Forum Commoner
Posts: 97
Joined: Tue Nov 09, 2004 8:32 am

Empty field

Post by snicolas »

Is there a quick way to check if 1 or more field is empty.
I am talking php and MSSQL.

thx.

S
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

in what the database or in the script before it gets into the db
snicolas
Forum Commoner
Posts: 97
Joined: Tue Nov 09, 2004 8:32 am

Post by snicolas »

The db contain records.
I need to check in php if 1 or more field is empty for a recordID.
If all empty i need to display something, if there is 1 field not empty, i need to dispaly something else.

Thx

S.
snicolas
Forum Commoner
Posts: 97
Joined: Tue Nov 09, 2004 8:32 am

Post by snicolas »

Anybody??please...
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Either check to see if the field is == '' or NULL depending on how your database is set up.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Once you have your row, if it is an array you could use the array functions to test it, such as

Code: Select all

if (in_array('',$row)!==false) {
   // You have empty
}
Not sure what would happen with null values, you would have to check.
snicolas
Forum Commoner
Posts: 97
Joined: Tue Nov 09, 2004 8:32 am

Post by snicolas »

thx CoderGob..this did the trick...
User avatar
php_passerby
Forum Newbie
Posts: 6
Joined: Fri Oct 03, 2003 9:41 am
Location: El Salvador

Post by php_passerby »

If you want to check within the script, I think the best way to do it would be to create an array (associative or enumerated) with

$myArray = mssql_fetch_array($myRecordset);
or
$myArray = mssql_fetch_row($myRecordset);

(I would prefer to use the first form)

Then, you should use a foreach control structure to increment a counter for each null value encountered, like this:

$nullFieldsEncountered = 0;
foreach ($myArray as $key, $value) {
if ($value=="") $nullFieldsEncountered++;
}

At the end of the iteration, you have the number of nulls in the variable!
Post Reply