1

enter image description herehere my RetriveData.Php:

<?php
ini_set("display_errors","on");
include("connection.php");

$query= "SELECT * FROM Person" ; //replace  with your table name
$result = mysqli_query($con,$query)  or  die("Error".mysqli_error($con));
//create an array
$json = array();
if(mysqli_num_rows($result))
{
  while($row=mysqli_fetch_row($result))
   {
      $json[]=$row;
   }
}
  echo json_encode($json);
 ?>

here id my .m file code :

NSError *error = nil;
NSString *url_string = [NSString stringWithFormat: @"http://127.0.0.1/RetriveData.php"];
NSData *data = [url_string dataUsingEncoding:NSUTF8StringEncoding];
NSMutableArray *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
NSLog(@"%@",error);
NSLog(@"json: %@", json);

The problem is this json array print Null.

Yun CHEN
  • 6,450
  • 3
  • 30
  • 33
lived
  • 23
  • 10
  • What is your question? – Sheldon Sep 12 '17 at 11:31
  • null returns in json array . but with the same url i can see json data on browser – lived Sep 12 '17 at 11:32
  • The response returned by your WS is not JSON valid (the one we see in the screenshot). You can test it on an online JSON validator, you'll see. That's why you have this error. – Larme Sep 12 '17 at 11:52

1 Answers1

0

First, remove text "Successfully connect" in your PHP code. And the result is not in json format so far. Try to improve it.

Second , at iOS side, you directly use url string as JSON. The right way is to get data from the url, and then convert it to JSON. Try:

NSError *error = nil;
NSString *url_string = @"http://127.0.0.1/RetriveData.php";
NSURL *url = [NSURL URLWithString:url_string];
if (url != nil) {
    NSData *data = [NSData dataWithContentsOfURL:url];
    NSMutableArray *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
    NSLog(@"%@",error);
    NSLog(@"json: %@", json);
}
Yun CHEN
  • 6,450
  • 3
  • 30
  • 33
  • Here is output Error: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.} json: (null) – lived Sep 12 '17 at 11:44
  • @lived, it's so strange, because the result from browser is not json. I think you need to improve it. – Yun CHEN Sep 12 '17 at 11:54
  • This maybe helpful: https://stackoverflow.com/questions/3351882/convert-mysqli-result-to-json/3352550 – Yun CHEN Sep 12 '17 at 13:31