Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8258347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:30:34+00:00 2026-06-08T02:30:34+00:00

I am trying to fetch data and encode it to JSON. I have this

  • 0

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);

}
 ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T02:30:37+00:00Added an answer on June 8, 2026 at 2:30 am

    Because $city is 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 the global $city at the beginning of your function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Iam trying to return json data from codeigniter view. but my code is not
I'm trying to fetch some JSON data from my server like so: var a
I have been trying to add data from my ajax query that returns json
I'm trying to save the data retrieved from the database into a .json. This
I have this JSON code: { phrases: [ { phrases: [ { id: 33,
I am pulling data from my database and trying to encode into JSON data
Been reading up on php and json and am trying this piece of code
I'm trying to pull data from my database using json in php. I have
I have the following code in my main page, trying to pull AJAX JSON.
I'm trying to fetch data from a page that resides on an intranet. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.