开发者

Check if value is null, if it is , print nothing, else, print value. Help?

Right, I'm still new to PHP/MySQL and what I've got, even in my head, sounds like it should be simple, b开发者_如何学Gout I just can't work out how to write it, here's what I want to happen:

MySQL statement get's the value's of all the cells where the row = id, PHP then prints them in the appropriate places (this bit I can do), If a value in said row is null, I don't want it to print ANYTHING related to that value, Else, continue as normal.

I.e:

-- Other code --

if (value where id = id == null) {

}
else {
Twitter: @Twitter
}


What is your exact problem? A simple if statement would do just fine.

$result = mysql_query("SELECT * FROM somewhere");

while($row = mysql_fetch_assoc($result))
{
 if($row['someValue'] != '')
 {
  echo $row['someValue'];
 }
}


Using SQL you can limit the number of rows that the program has to fetch. To ignore all rows where any of the column values is null, you define a IS NOT NULL condition for each column you require to be not null. For example

SELECT id, label value 
FROM my_very_special_Table
WHERE id = ?  
  AND label IS NOT NULL
  AND value IS NOT NULL

Now there's always a possibilty you have empty string or strings with only white space. MySQL seems to distinguish an empty string from a null value unlike at least Oracle i.e. '' <> NULL is true.

In this case you'll have to modify the conditions to make things work properly with VALUE <> '' or LENGTH(VALUE) > 0. You can add trimming to clear spaces, but in MySQL, trim doesn't remove any other white-space characters. You can also match agains a regular expression but that's probably a bit slow.

If you happen to have both null values and empty strings, you have to cover both scenarios.


It depends on how you get the data from MySQL into PHP, but if you use a funcion like mysql_fetch_array(), you're value in PHP will be NULL as well:

if ([value where id = id] !== null) {
    Twitter: @Twitter;
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜