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'