Do not echo in an empty row IS NOT NULL

1

I came across a problem that I can not work around. The situation is as follows, I created a search engine in mysql that results in map markup (Google Maps API) that I created. The problem is that some of the clients have blank "lat" and "lon" fields, resulting in an error in the Maps API, which is all blank. The idea is that if a client has the "lat" and "lon" fields blank, they do not echo this client. This would not result in map errors. And the other clients with all fields filled, would normally echo. I've tried using IS NOT NULL, but it does not work. PHP Code:

$sql = mysql_query("SELECT * FROM politicos WHERE name LIKE '%$busca%' OR info LIKE '%$busca%' AND lat IS NOT NULL");
while($row = mysql_fetch_array($sql)){
$name = $row['name'];
$lat = $row['lat'];
$lon = $row['lon'];
$info = $row['info'];
echo("addMarker($lat, $lon, '<b>Nome: $name</b><br />Informações: $info');\n");
}

echo result:

addMarker(, , '<b>Nome: Cliente 1</b><br />Informações: ');

This results in an error on the map

echo result if you put 0 in lat and lon:

addMarker(0, 0, '<b>Nome: Cliente 1</b><br />Informações: ');

This places the marker on the equator line: (

I do not want to echo customers with the empty "lat" and "lon" fields! If anyone can help me thank you very much!

    
asked by anonymous 09.06.2017 / 03:31

3 answers

2

Try to use the empty function.

The function empty returns FALSE for all items below:

  
  • "(an empty string)
  •   
  • 0 (0 as an integer)
  •   
  • 0.0 (0 as a floating point)
  •   
  • "0" (0 as a string)
  •   
  • NULL
  •   
  • FALSE
  •   
  • array () (an empty array)
  •   
  • $ var; (a declared variable but no value)
  •   

It would look like this:

<?php

/*...*/

while($row = mysql_fetch_array($sql))
{
    $name = $row['name'];
    $lat = $row['lat'];
    $lon = $row['lon'];
    $info = $row['info'];

    if( !empty($lat) && !empty($lon) )
    {
        echo("addMarker($lat, $lon, '<b>Nome: $name</b><br />Informações: $info');\n");    
    }
}
    
09.06.2017 / 03:53
0

There are several ways to verify this, depending on what you return from the bank. To test, use var_dump($row['lat']); to identify the type of return you have.

After identifying, one of these cases below should answer you

if (isset($row['lat'])) {
  echo("addMarker....")
}

or

if ($row['lat'] !== '') {
  echo("addMarker....")
}

or

if ($row['lat'] !== NULL) {
  echo("addMarker....")
}

something in this line ...

    
09.06.2017 / 03:44
0

The appropriate form, via PHP, is as responded @BrunoRigolon.

It can also only resolve in the SQL query.

The problem is that NULL is different from empty. and probably the fields are not NULL but empty.

For consistency, make the condition

(lat IS NOT NULL AND lat != '')

* Parentheses are required because there are two conditions.

    
09.06.2017 / 06:35