简体   繁体   中英

jsonFile PHP server problems

I'm trying to get some server data to dump into a table on my iphone. The flow is standard server - php_interface - iOS. I was trying an echo json_encode(array) setup but when doing that I wasn't able to populate the array, so (with excellent help from stack users) I switched setup and forwent the json_encode(array) setup. However I now realise that I need if for the iOS connection. I would like to simply just input it at the end of my current PHP code but that doesn't seem to work. I am quite confident the code on the iOS side is working because the build shows an array it simply isn't populating, and the error message I am getting in xCode is saying that it found 0 rows. Help please?

Current code (that I am trying to get to work):

        $mysqli = mysqli_connect($dbHOST, $dbUSER, $dbPASS, $dbNAME);
        $query = "SELECT Name, Address, Latitude, Longitude FROM Locations";
        $result = mysqli_query($mysqli, $query);

        if($result) {
            while ($row = mysqli_fetch_assoc($result)) {
                echo 'Name: ' . $row['Name'] . '<br />';
                echo 'Address: ' . $row['Address'] . '<br>';
                echo 'Latitude: ' . $row['Latitude'] . '<br>';
                echo 'Longitude: ' . $row['Longitude'] . '<br>';    
                echo ''. '<br>';

    json_encode($result);
            }
        }

Old code snippet that never worked fully:

    if (mysqli_connect_errno()) {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $sql = "SELECT * FROM `Locations` ";

    if ($result = mysqli_query($con, $sql))
    {
     $resultArray = array();
     $tempArray = array();

     while($row = $result->fetch_object(Locations)){

      $tempArray = $row;
        array_push($resultArray, $tempArray);
     }

     echo json_encode($resultArray);
    }

    // Close connections
    mysqli_close($con);
    ?>

xCode metod for getting the rows:

    - (void) downLoadItems{
        // Download the json file
        NSURL *jsonFileUrl = [NSURL URLWithString:@"http://server.com/service.php"];

        // Create the request
        NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:jsonFileUrl];

        // Create the NSURLConnection
        [NSURLConnection connectionWithRequest:urlRequest delegate:self];

    }

I messed up ans simply parsed Locations in the PHP method. Remove that and the "old" code works fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM