drzfz9995 2015-06-14 20:10
浏览 27
已采纳

无法使用查询值创建数组

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();
                }
  • 写回答

1条回答 默认 最新

  • doubei3312 2015-06-14 21:05
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染