0

Right, I'm still new to PHP/MySQL and what I've got, even in my head, sounds like it should be simple, but 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
}
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
AviateX14
  • 760
  • 4
  • 18
  • 36

3 Answers3

3

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'];
 }
}
TJHeuvel
  • 12,403
  • 4
  • 37
  • 46
  • There's always a possibility that the database contains values that are nothing but white-space. A `trim` might help in that case either in PHP or in SQL. In MySQL trim only apparently works on spaces though. – Aleksi Yrttiaho Aug 11 '11 at 10:53
  • I'd have to agree, try to check in your SQL query whether the particular field is empty. This will be more performant. – TJHeuvel Aug 11 '11 at 11:04
2

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.

Community
  • 1
  • 1
Aleksi Yrttiaho
  • 8,266
  • 29
  • 36
0

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;
Oscar
  • 65
  • 6