Swift JSON parsing error with PHP/mySQL data -


i trying parse json objects returned php/mysql script utilizes post method variables in php script.

half time run viewcontroller, errors out (thread 1:exc_bad_instruction) , doesn't return json, half time works. i'm unsure why it's doing this. error occurs when it's parsing "grabid.php" , error occurs when it's parsing "loadsongsintoparty.php"

here viewdidload

    override func viewdidload() {     super.viewdidload()       partynamelabel.text = recievedpartyname     pinlabel.text = recievedpin      println(self.recievedpin)     println(self.recievedhost)     println(self.recievedgenre)      let iddata = "pin=\(self.recievedpin)&host=\(self.recievedhost)&genre=\(self.recievedgenre)"      let idurl: nsurl = nsurl(string: "http://myserverurl/grabid.php")!     var idpostdata:nsdata = iddata.datausingencoding(nsutf8stringencoding)!      var idrequest: nsmutableurlrequest = nsmutableurlrequest(url:idurl)     idrequest.httpmethod = "post"     idrequest.httpbody = idpostdata      var idresponseerror: nserror?     var idresponse: nsurlresponse?      var idurldata: nsdata? = nsurlconnection.sendsynchronousrequest(idrequest, returningresponse: &idresponse, error: &idresponseerror)      var ids: anyobject! = nsjsonserialization.jsonobjectwithdata(idurldata!, options: nsjsonreadingoptions(0), error: nil)      println(ids)       if let idjson = ids as? array<anyobject> {          index in 0...idjson.count-1 { //where error occurs              let songid : anyobject? = idjson[index]              let songids = songid! dictionary<string, anyobject>              let id : anyobject! = songids["id"]              self.partyid.removeall(keepcapacity: false)              self.partyid.append(id string)           }       }      println(self.partyid[0])      idlabel.text = self.partyid[0]      let queryid = self.partyid[0]     let querygenre = self.recievedgenre      let loadintovotes = "queryid=\(queryid)&querygenre=\(querygenre)"      let url: nsurl = nsurl(string: "http://myserverurl/~team11/loadsongs.php")!     let request:nsmutableurlrequest = nsmutableurlrequest(url:url)     request.httpmethod = "post"      request.httpbody = loadintovotes.datausingencoding(nsutf8stringencoding);     nsurlconnection.sendasynchronousrequest(request, queue: nsoperationqueue.mainqueue())         {             (response, data, error) in      }      // mysql json parsing script         let loadsongsid = self.partyid[0]       let bodydata = "partyid=\(loadsongsid)" //to them in php: $_post['name']       let songurl: nsurl = nsurl(string: "http://myserverurl/loadsongsintoparty.php")!     var postdata:nsdata = bodydata.datausingencoding(nsutf8stringencoding)!      var songsrequest: nsmutableurlrequest = nsmutableurlrequest(url:songurl)     songsrequest.httpmethod = "post"     songsrequest.httpbody = postdata      var responseerror: nserror?     var response: nsurlresponse?      var urldata: nsdata? = nsurlconnection.sendsynchronousrequest(songsrequest, returningresponse: &response, error: &responseerror)      var allsongs: anyobject! = nsjsonserialization.jsonobjectwithdata(urldata!, options: nsjsonreadingoptions(0), error: nil)       if let songsjson = allsongs as? array<anyobject> {          println(songsjson)           index in 0...songsjson.count-1 { //where error occurs              let indivsong : anyobject? = songsjson[index]              let collection = indivsong! dictionary<string, anyobject>              let track : anyobject! = collection["track"]             let artist : anyobject! = collection["artist"]             let trackuri : anyobject! = collection["trackuri"]             let vote: anyobject! = collection["vcount"]              self.tracks.append(track string)             self.names.append(artist string)             self.tracksuri.append(trackuri string)             self.votes.append(vote string)          }       } 

here grabid php script:

 <?php   // create connection                                                              $con=mysqli_connect("#####", "#####", "#####", "#####");    // check connection                                                               if (mysqli_connect_errno())  {  echo "failed connect mysql: " . mysqli_connect_error();   }   // change character set utf8                                                   mysqli_set_charset($con,"utf8");   $partypin = $_post['pin'];  $host = $_post['host'];  $genre = $_post['genre'];    $sql = "select id party pin = '$partypin' , host = '$host' ,  genre = '$genre'";   // check see if there results                                              if ($result = mysqli_query($con, $sql))   {   // if so, create results array , temporary 1 hold date             $resultarray = array();  $temparray = array();   // loop through each row in result set                                        while($row = $result->fetch_object())  {  //add each row our results array                                             $temparray = $row;  array_push($resultarray, $temparray); }   // finally, encode array json , output results                       echo json_encode($resultarray);  }    mysqli_close($con)  ?> 

here loadsongsintoparty php script:

 <?php   // create connection                                                              $con=mysqli_connect("#####", "#####", "######", "######");    // check connection                                                               if (mysqli_connect_errno())  {  echo "failed connect mysql: " . mysqli_connect_error();  }   // change character set utf8                                                   mysqli_set_charset($con,"utf8");   $id = $_post['partyid'];   $sql = "select track, artist, trackuri, vcount votes pid = '$id'";   // check see if there results                                              if ($result = mysqli_query($con, $sql))   {   // if so, create results array , temporary 1 hold date             $resultarray = array();  $temparray = array();   // loop through each row in result set                                        while($row = $result->fetch_object())  {  //add each row our results array                                             $temparray = $row;    array_push($resultarray, $temparray);  }   // finally, encode array json , output results                       echo json_encode($resultarray);  }    mysqli_close($con)   ?> 

 


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -