1

I'm new to php and I'm trying to create a webservice. I have a function to select a number of rows from a database which preforms correctly (it returns the 22 rows) but when I call that function from my index.php page and output to JSON I only get the first row being outputted.

Can anyone point me in the correct direction?

MySQL function:

    public function getStops()
    {
        //Perform query
        $result = mysql_query("SELECT station_name FROM station WHERE line =   2") or die(mysql_error());

        //Check result
        $numRows = mysql_num_rows($result);

        if($numRows > 0)
        {
            return mysql_fetch_array($result);
        }
        else
        {
            return false;
        }
    }

Code in index.php calling getStops() function:

            //Check tag request
    if ($tag == "greenLineStations")
    {
        $greenStops = $db->getGreenLineStops();

        if ($greenStops != false)
        {
            //Get data and set success = 1
            $response["success"] = 1;
            foreach ($greenStops as $value)
            {
            $response["stops"]['name'] = $value;
            }
            echo json_encode($response);
        }
        else
        {
            //Set error = 1 and out error message
            $response["error"] = 1;
            $response["error_message"] = "Error occured while retreiving stations";
            echo json_encode($response);
        }
    }
    else
    {
        echo "Invalid Request";
    }
} 

JSON dump:

    {"success":1,"error":0,"stops":[{"name":{"0":"St. Stephen's Green","station_name":"St. Stephen's Green"}},{"name":{"0":"Harcourt","station_name":"Harcourt"}},{"name":{"0":"Charlemont","station_name":"Charlemont"}},{"name":{"0":"Ranelagh","station_name":"Ranelagh"}},{"name":{"0":"Beechwood","station_name":"Beechwood"}},{"name":{"0":"Cowper","station_name":"Cowper"}},{"name":{"0":"Milltown","station_name":"Milltown"}},{"name":{"0":"Windy Arbour","station_name":"Windy Arbour"}},{"name":{"0":"Dundrum","station_name":"Dundrum"}},{"name":{"0":"Balally","station_name":"Balally"}},{"name":{"0":"Kilmacud","station_name":"Kilmacud"}},{"name":{"0":"Stillorgan","station_name":"Stillorgan"}},{"name":{"0":"Sandyford","station_name":"Sandyford"}},{"name":{"0":"Central Park","station_name":"Central Park"}},{"name":{"0":"Glencairn","station_name":"Glencairn"}},{"name":{"0":"The Gallops","station_name":"The Gallops"}},{"name":{"0":"Leopardstown Valley","station_name":"Leopardstown Valley"}},{"name":{"0":"Ballyogan Wood","station_name":"Ballyogan Wood"}},{"name":{"0":"Carrickmines","station_name":"Carrickmines"}},{"name":{"0":"Laughanstown","station_name":"Laughanstown"}},{"name":{"0":"Cherrywood","station_name":"Cherrywood"}},{"name":{"0":"Brides Glen","station_name":"Brides Glen"}}]}

2 Answers 2

2
   if($numRows > 0)
   {
      $return = array();
      while($array = mysql_fetch_array($result)){
          $return[] = $array;
      }
      return $return;
   }

and :

        $response["success"] = 1;
        $response['stops'] = array();
        foreach ($greenStops as $value){
            $response["stops"][]['name'] = $value[0];
        }
Sign up to request clarification or add additional context in comments.

2 Comments

This helps a lot thankyou but it gives me the data twice. It outputs the root node and a secondary node for each station
Sure, I added it above
0

It looks like your foreach loop in the index.php is just overwriting the same index over and over again. Depending on how you want the output to be structured I would suggest changing the line 12 to:

$response["stops"][]['name'] = $value;

1 Comment

Thanks for your answer this combined with Loic's answer gets me a lot further but it outputs the data twice, I seem to be getting the root node and a secondary node outputted each time for each row

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.