sforman
sforman

Reputation: 17

How can i check if a single mysql field is empty in php

After getting the user-info from my sql database I would like to check if some of the fields are empty and continue the script based on that. A simplified piece of code would look like this:

$userData = mysql_query("SELECT * FROM user WHERE user='".$user."' LIMIT 1");

if(mysql_num_rows($data) == 1){

    $u_info = mysql_fetch_assoc($data);

    if(empty($u_info['u_mobile'])){
        echo 2;
        exit();
    } else {
        echo 1;
        exit();
    }

} else {

    echo 3;
    exit();
}

The problem is the empty statement checking the recieved field. I've tried using empty(), isset(), not_null() and array_key_exists() with no luck and can't get around to what I'm doing wrong. I also tried if($u_info['u_mobile']) == '' || $u_info['u_mobile']) == NULL) but that doesnæt work either.

Why is this, or how can I go about getting this information?

I need to collect the user-information and send them to fill out the information I don't have...

Upvotes: 0

Views: 195

Answers (2)

Techno
Techno

Reputation: 5

$userData = mysql_query("SELECT * FROM user WHERE user='".$user."' LIMIT 1");

if(mysql_num_rows($userData ) == 1){

    $u_info = mysql_fetch_assoc($userData );

    if(empty($u_info['u_mobile'])){
        echo 2;
        exit();
    } else {
        echo 1;
        exit();
    }

} else {

    echo 3;
    exit();
}

Please Run code..I think it will be compile better it was minor mistake

Upvotes: 0

degenerate
degenerate

Reputation: 1427

You're setting the query result to $userData but then you're using mysql_fetch_assoc($data); -- doh. You need to pass the variable that you set the query result to:

$u_info = mysql_fetch_assoc($userData);

It's OK, it is still 10AM EST so this can happen in the morning =) I suggest that you turn on PHP error reporting. PHP would have alerted you that the array values were trying to access do not exist, saving you a lot of wasted frustration.

Upvotes: 2

Related Questions