开发者

Assigning a default value to a blank recalled database field

I'm working on an attempted social networking site with a relative of mine as a side project. I'm writing most of it bymyself. I have made it so each user is stored into a table with their data (mysql) and i want to do a single compare statement so that if a field is blank to change the recieved variable to "No data".

For example: persons data is

name=john
age=34
mobile number=(empty because it hasnt been entered)

so when i receive the resource id with the mysql_func_assoc function $result= mysql_query($sql);

$db_field = mysql_fetch_assoc($result);
$szUser_name=$db_field['user_name'];
$szAge=$db_field['user_age'];
$szNumber=$db_field['user_number'];

now i want an if statement to recognise a blank field (something like if($szUser=="" ||$szAge=="" ||$szNumber=="")) and then automatically change the respective field to "No data". i could just set all the values to ="No data" above and have开发者_如何学运维 them reassigned by the assoc function but im looking for a neat simple option. Thanks in advance


Maybe put this into a function so you can reuse the code?

function getField($fieldName, $fields, $default="No data") {
   return empty($fields[$fieldName]) ? $default : $fields[$fieldName];
}

So

$szNumber=$db_field['user_number'];

becomes

$szNumber=getField('user_number', $db_fields);

EDIT: Yeah you're right

//Nitpick : Add an 's' to $db_field as well since it contains multiple fields. Or consider renaming it $db_row


If your "empty" fields are actually set to NULL:

For a pure MySQL solution you can use COALESCE to achieve what you want assuming the "empty" fields are set to NULL. For example:

SELECT COALESCE(`user_name`, 'No data') AS `username` FROM `users`

IFNULL works in a similar way and is probably better suited to this particular job than COALESCE:

SELECT IFNULL(`username`, 'No data') AS `username` FROM `users`

COALESCE can have many arguments and simply returns the first NOT NULL argument whereas IFNULL only accepts two arguments and it takes the first NOT NULL argument.

If your "empty" fields are actually empty strings:

If your values are empty strings rather than NULL then you can use:

SELECT IF('' = `username`, 'No data', `username`) AS `username` FROM `users`

For more information on the MySQL IF function see the manual.

If you have a combination of both empty strings and NULL:

You can use:

SELECT IF('' = `username` OR `username` IS NULL, 'No data', `username`) AS `username` FROM `users`


This is usually the kind of thing where you use the ternary operator :

$szNumber = (!empty($db_field['user_number'])) ? $db_field['user_number'] : 'No data';

Or, you can use the full if/else block.

You could also a CASE statement in your query :

SELECT CASE WHEN user_number = '' THEN 'No data' ELSE user_number END AS user_number
FROM yourtable

But it can make queries hard to read.


The best choice is to choose MVC-based architecture for your application, transfer raw sql-data from controller into view, and to decide what data to output in view part. For example, Zend Framework allows to use classes called "view helpers", which will display "No data" instead of empty strings.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜