I am trying to fetch data and encode it to JSON. I have this very confusing trouble. The code I have put in getAnnotions() function, when I do not put it in function, the while loop (commented as //This loop) is reached. Whereas when I encapsulate the same code in getAnnotions() function, that while loop is not reached. What might be the problem?
Here is the code:
<?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);
}
?>
Because
$cityis defined in the global scope, and in PHP functions variables of another scope cannot be used directly. You can either pass it as a parameter (suggested), or use theglobal $cityat the beginning of your function.