-2

我正在尝试获取数据并将其编码为 JSON。我有这个非常混乱的麻烦。我放在 getAnnotions() 函数中的代码,当我不把它放在函数中时,会到达 while 循环(注释为 //This loop)。而当我将相同的代码封装在 getAnnotions() 函数中时,不会到达该 while 循环。可能是什么问题?

这是代码:

    <?php

$city=$_GET["city"];
//$limit="1";
//$place=$_GET["place"];

getAnnotions("1");

function getAnnotions($limit)
{

    $con = mysql_connect("localhost","hidden","*******");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }

    mysql_select_db("merrycod_tummy", $con);

    $result = mysql_query("SELECT * FROM deal where city_names LIKE '%".$city."%'");

    $rows = array();

    while($row = mysql_fetch_array($result))
      {

          $rows[] = $row;


          $result2 = mysql_query("SELECT locationLat,locationLong FROM place where city ='".$city."' AND name='".$row['place_name']."' LIMIT ".$limit);



          while($row2 = mysql_fetch_array($result2))
          {
                $rows[] = $row2;

              //This loop

          }


      }

      echo json_encode($rows);


    mysql_close($con);

}
 ?>
4

2 回答 2

4

因为$city是在全局范围内定义的,在 PHP 函数中不能直接使用另一个范围的变量。您可以将其作为参数传递(建议),也可以global $city在函数的开头使用。

于 2012-07-21T12:22:39.160 回答
0

建议你先回显$result中的sql,然后运行你在phpmyadmin中回显的代码,看看你的sql是否正确

于 2012-07-21T12:23:03.807 回答