brabus85
brabus85

Reputation: 71

Fail to create array with query values

I try to save the query values into an array, but it wont become filled. The query should give something out because any values in the database accomplish the terms and the cookies have too a value. Where is my mistake?

Output:

Array ( )

    $range = 'range';
  $_COOKIE["$range"];

$longitude = 'longitude';
$_COOKIE["$longitude"];
$latitude = 'latitude';
$_COOKIE["$latitude"];

$onemile = 0.005581257;
$le = $range * $onemile; 
$lo = $longitude + $le;
$loo = $longitude - $le;
$la = $latitude + $le;
$laa = $latitude - $le;


$hostname='localhost';
        $user='root';
        $password='';



                try {
                        $dbh = new PDO("mysql:host=$hostname;dbname=loc",$user,$password);

                        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
                         $sql = "SELECT id, autorid, date, longitude, latitude, title, text 
FROM post 
WHERE  (
    longitude >= $loo and longitude <= $lo
) 
OR (
    latitude >= $laa and latitude <= $la
) 
ORDER BY date";
  if ($res = $dbh->query($sql)) {

     $result = $res->fetchAll();
     print_r($result);

   }


                }
                catch(PDOException $e)
                {
                        echo $e->getMessage();
                }

Upvotes: 0

Views: 30

Answers (1)

Barmar
Barmar

Reputation: 781004

Your variable assignments are all wrong. They should be:

$range = $_COOKIE["range"];
$longitude = $_COOKIE["longitude"];
$latitude = $_COOKIE["latitude"];

When you try to use a non-numeric string as a number in an arithmetic expression, it's treated as 0. So the result of your code was effectively:

$le = 0 * $onemile; 
$lo = 0 + $le;
$loo = 0 - $le;
$la = 0 + $le;
$laa = 0 - $le;

so you were setting all these variables to 0.

Upvotes: 1

Related Questions