0

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();
                }
12
  • Does the query output/execute correctly if you echo it? Separate things you should know... You should use prepared statements, you also should look at the between mysql function. Commented Jun 14, 2015 at 20:18
  • WHERE ( longitude between $loo and $lo or latitude between $laa and $la ) doesnt change something Commented Jun 14, 2015 at 20:25
  • Yes, I said Separate things. First find why your query isn't working then fix the other issues. Does the query work if you output it and execute on your db? Commented Jun 14, 2015 at 20:28
  • 1
    What I'm seeing is you're storing the string 'range' to the variable $range and then multiplying it by the value of $onemile, which is 0.00558... Not sure what you expect to get. Commented Jun 14, 2015 at 20:41
  • 1
    None of those assignments are right. What are you expecting? Commented Jun 14, 2015 at 21:04

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.