0

I have a script that geocodes physical addresses and puts it into a MySQL database with two columns: coord_lat and coord_long. These are float type columns but unfortunately some addresses don't get geocoded correctly and the script tries to push the address as a null value into the database which then breaks the script because the database cannot hold a null value.

I'm trying to find a way I can rewrite my script to determine if the geocoded address comes out to a null value to automatically rewrite that value to 0 so that the database and script don't break.

Can anyone give me some advice on switching/replacing a null values to 0?

asontu
  • 4,548
  • 1
  • 21
  • 29
chronotrigga
  • 589
  • 1
  • 10
  • 33
  • Can you show the code where the default value of `null` is being input? At face value it sounds like you should default those two values to `0` and then overwrite them with the correct value after creating the geocode values. – Crackertastic Mar 23 '15 at 14:19
  • 0,0 is a real place on the earth. Maybe you want to use 91,0. – O. Jones Mar 23 '15 at 15:27

2 Answers2

3

try this:

if PHP (shorthand if)

(is_null($value) ? 0:$value)

else if MySQL (coalesce function)

SELECT COALESCE(@your_value_here,0) FROM dual

for reference of coalesce: http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html

for shorthand if statement (ternary):http://davidwalsh.name/php-ternary-examples

Arzgethalm
  • 516
  • 5
  • 14
1

If the value is NULL, then assign 0

if(is_null($value)){
  $value = 0;
}
paddyfields
  • 1,466
  • 2
  • 15
  • 25