1

I'am developing an iPhone application to display data from php. Data are obtained from a Mysql database then encoded to JSON format in php file:

include_once 'connectionIncl.php';

if(function_exists($_GET['method'])){
    $_GET['method']();

}
function getSQLIntoJSONFormat()
{
    $arr;
    $sql = mysql_query("SELECT * FROM pecivo");
    while($pecivo = mysql_fetch_assoc($sql)){
        $arr[] = $pecivo['typ'];
    }
    $arr= json_encode($arr);
    echo $_GET['jsoncallback'].'('.$arr.')';
}

// --- http://127.0.0.1:8887/TeplyRohlik/pecivo.php?method=getSQLIntoJSONFormat&jsoncallback=?

when i run this from browser, it returns correct data :

(["sejra","knir","baba","vousy","sporitelna25"])

Also, on iOS a have this code:

NSString * urlString = [NSString stringWithFormat:@"http://192.168.0.10:8887/TeplyRohlik/pecivo.php?method=getSQLIntoJSONFormat&jsoncallback=?"];
NSURL * url = [NSURL URLWithString:urlString];
NSData * data = [NSData dataWithContentsOfURL:url];
NSError * error;
NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(@"%@",json);

And result is .... (null). I have no idea how to get this working...

7
  • have you tried if the URL is reachable from your device? Go to your phone-browser and type the url above to see if it's answering correctly Commented Jul 21, 2012 at 17:07
  • Please don't use mysql_* functions in new code. They were removed from PHP 7.0.0 in 2015. Instead, use prepared statements via PDO or MySQLi. See Why shouldn't I use mysql_* functions in PHP? for more information. Commented Jul 21, 2012 at 17:07
  • Why don't you look at the NSError object to find out more info? o,0 Commented Jul 21, 2012 at 17:10
  • it will probably help to log the data (the http response) and the error from the json parser Commented Jul 21, 2012 at 17:11
  • 1
    Another thing I noticed is that your container is not a dictionary. It is an array!! Commented Jul 21, 2012 at 17:13

1 Answer 1

2

It looks like your PHP method is spitting out JSONP. What you probably want to do is change that to:

function getSQLIntoJSONFormat()
{
    $arr;
    $sql = mysql_query("SELECT * FROM pecivo");
    while($pecivo = mysql_fetch_assoc($sql)){
        $arr[] = $pecivo['typ'];
    }
    $arr= json_encode($arr);
    echo $arr;
}

You are seeing the output be wrapped in parentheses as it's expecting a GET parameter in the request called jsoncallback which would make the output look something like this:

javascriptFunction(["a","b","b"])

That's not what you want on your iOS device. You want just the raw JSON string of the array, no wrapping in a callback function call.

Sign up to request clarification or add additional context in comments.

Comments

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.