php判断mysql空值,使用PHP检查MySQL NULL值

This is how my table looks like..

id col1 col2

---------------

1 a x

2 NULL y

3 NULL z

4 NULL t

col1 has a default value of NULL.

I want to use col1 data If col1 is not null, otherwise use col2 data.

function something($col1,$col2)

{

if(is_null($col1) == true)

$var = $col2

else

$var = $col1

return $var;

}

function something2($col1,$col2)

{

if($col1 === NULL)

$var = $col2

else

$var = $col1

return $var;

}

Here is my problem. Both of these functions returns $col2 values. But as you can see in first row, col2 column is not null. What am I doing wrong? UPDATE: Looking for a solution with PHP and I need both col1 and col2 values.

Also I want to learn, Does using NULL values is the best practice for this example?

解决方案

I see a slew of problems in your question:

I want to use col1 data If col2 is not null, If It's I will use col2 data.

I assume you mean you want to use col2 data if col1 IS null otherwise use col1. In that case you have issues in your php. Not sure if you provided sample code or not but you're not passing any variables to the function nor declaring them as global inside the func.

function something($col1, $col2){

if(is_null($col1) == true)

$var = $col2;

else

$var = $col1;

return $var;

}

function something2($col1, $col2){

if($col1 === NULL)

$var = $col2;

else

$var = $col1;

return $var;

}

echo something('a','x');

echo something2('a','x');

This gives you 'a' in both cases

echo something(NULL,'b');

echo something2(NULL,'b');

This gives you 'b'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值