Do not echo an empty Row IS NOT NULL

Asked

Viewed 165 times

1

I ran into a problem I can’t get around. The situation is as follows, I created a search system in mysql that results in markings on the map (Google Maps API) that I created. The problem is that some of the customers have the blank"lat" and "Lon" fields, and this results in an error in the Google Maps API, which is all blank. The idea is that if any client has blank "lat" and "Lon", not echo fields on this client. This would not result in errors on the map. And the other clients with all fields filled would echo normally. I’ve tried using IS NOT NULL, but it doesn’t 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 error on the map

Echo result if put 0 in lat and Lon:

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

that puts the marker on the equator line :(

I do not want to echo into customers with the fields "lat" and "Lon" empty! If anyone can help me, I’d really appreciate it!

  • A ideia é que se algum cliente estiver com os campos "lat" e "lon" em branco, não de echo neste cliente. see, empty and null are different things. The fields in bd get null values, even?

  • Try adding parentheses in the conditions that involve like, guy WHERE (name LIKE ... OR info LIKE ...) AND lat IS NOT NULL. Enjoy and read Why should we not use mysql type functions_*?.

3 answers

2


Try to use the function empty.

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)

Would look this way:

<?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");    
    }
}

0

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

After identifying, any of these cases below should suit you

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

or

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

or

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

something along those lines...

0

The appropriate way, via PHP, is as answered @Brunorigolon.

You can also resolve only in the SQL query.

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

To give consistency, make the condition

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

*The parentheses are necessary because there are two conditions.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.