March 7, 2013

Checking Array for null element

Question by V1rtua1An0ma1y

So I’ve created my own class with 5 private fields: Each is an array with a pre-set length. It’s my way of creating a table, where each array is a column, and they have pre-set lengths because not every cell will contain an element (So, not using anything dynamic).

Anyway, my question is: Can I check to see if a specific cell of a specific array contains “null”? Using .equals(null) gives a nullpointerexception 🙁

Answer by user000001

When you call .equals(...) you call a method of the object. If it is null, it has no method. Therefore null check like this:

if (myArray[position] == null) {
    ....

}

Answer by Starx

Mixed up for loops and null construct

for(Integer ints : intNum) {
    if(intNum != null) {
      //valid
    }
} 
April 10, 2012

Only update fields that are not null

Question by user1325305

I have been trying to figure out how to update the fields only if they are not null (otherwise NULL overrides the unchanged fields). It doesn’t matter that the fields can not be null.

I have tried both the COALESCE and ISNULL, however I must not be using them in the right way as I’m not use to the Dreamweaver coding. The code is below:

<i>if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "catalogue")) {
  $updateSQL = sprintf("UPDATE catalogue SET catbox1=%s, catbox2=%s, catbox3=%s, catbox4=%s, catbox5=%s, catbox6=%s WHERE id=%s",
                       GetSQLValueString($_POST['catbox1'], "text"),
                       GetSQLValueString($_POST['catbox2'], "text"),
                       GetSQLValueString($_POST['catbox3'], "text"),
                       GetSQLValueString($_POST['catbox4'], "text"),
                       GetSQLValueString($_POST['catbox5'], "text"),
                       GetSQLValueString($_POST['catbox6'], "text"),
                       GetSQLValueString($_POST['id'], "int"));</i>

Answer by Starx

There are few mistakes in you code, But these can be typos.

  1. NO PHP Wrapper: You have include your mysqlcode with portion
  2. Use IS NOT NULL instead of ISNULL

Basic way of what you are trying to do is use a query like this

UPDATE `tablename` SET `fieldName` = 'value' WHERE `fieldName` IS NOT NULL
April 5, 2012

Is there a reason not to use <=> (null safe equals operator) in mysql instead of =?

Question by cellige

MySql provides a nice operator <=> that works with comparisons that could contain a null such as null <=> null or null <=> 5 etc giving back intuitive results as many programming languages. Where as the normal equals operator always just returns null, which catches many new MySql users such as myself awry.

Is there a reason MySql has both and not JUST the functionality in <=> ? Who really needs an operator that is effectively undefined with built in language types?

Answer by Starx

Is there a reason MySql has both and not JUST the functionality in <=>
?
The operators are completely different from each other.

<=> performs an equality comparison like the = operator, but returns 1 rather than NULL if both operands are NULL, and 0 rather than NULL if one operand is NULL.

Who really needs an operator that is effectively undefined with built
in language types?

This depends on case, just because you haven’t encountered such cases, does not mean nobody needs it.

...

Please fill the form - I will response as fast as I can!