php - Retrieve and use entire MYSQL Table in Swift -


i've looked around internet have yet come across solution seems should common practice.

i trying write method in swift which, using php backend, retrieves data specific mysql table in database.

in other sections of app (user login etc) have got similar system working, needs return few variables 1 specific user, can return these in json data individual variables , store them in nsuserdictionary use throughout app, easy!

however have table information different 'shops' each row storing various details (location, name etc). want able plot these names onto mkmapview , therefore need retrieve entire table , iterate through each row (shop) within swift. (well that's way can think of accomplishing this).

my issue can figure out how return '1d' json data in php (e.g. location / name etc specific shop), not entire array of shops?

this code i'm using in swift retrieve 1d data user:

        var post:nsstring = "email=\(email)&password=\(password)"          nslog("postdata: %@",post);          var url:nsurl = nsurl(string:"http://www.agentsweep.co.uk/scripts/agentsweep_user_login.php")!          var postdata:nsdata = post.datausingencoding(nsasciistringencoding)!          var postlength:nsstring = string( postdata.length )          var request:nsmutableurlrequest = nsmutableurlrequest(url: url)         request.httpmethod = "post"         request.httpbody = postdata         request.setvalue(postlength string, forhttpheaderfield: "content-length")         request.setvalue("application/x-www-form-urlencoded", forhttpheaderfield: "content-type")         request.setvalue("application/json", forhttpheaderfield: "accept")           var reponseerror: nserror?         var response: nsurlresponse?          var urldata: nsdata? = nsurlconnection.sendsynchronousrequest(request, returningresponse:&response, error:&reponseerror)          if ( urldata != nil ) {             let res = response as! nshttpurlresponse!;              nslog("response code: %ld", res.statuscode);              if (res.statuscode >= 200 && res.statuscode < 300)             {                 var responsedata:nsstring  = nsstring(data:urldata!, encoding:nsutf8stringencoding)!                  var error: nserror?                  let jsondata:nsdictionary = nsjsonserialization.jsonobjectwithdata(urldata!, options:nsjsonreadingoptions.mutablecontainers , error: &error) as! nsdictionary 

and backend php part returns it:

if ($result = $mysqli->query("select id, fullname, mobile, mobilepref, address, autofill users email = '$email';")) {                 $userdata = $result->fetch_assoc();                 $result->close();             }                $fullnameresult = $userdata["fullname"];             $idresult = $userdata["id"];             $mobileresult = $userdata["mobile"];             $mobileprefresult = $userdata["mobilepref"];             $addressresult = $userdata["address"];             $autofillresult = $userdata["autofill"];             /* close connection */             $mysqli->close();              if ($id) {                  error_log("user $email: password match.");                   /* reply data:*/                 /*echo '{"success":1,"id":'. $idresult .',"fullname":'. $fullname .',"mobile":'. $mobileresult .',"mobilepref":'. $mobileprefresult .',"address":'. $addressresult .'}';             */                 echo '{"success":1,"idref":"'. $idresult .'","fullname":"'. $fullnameresult .'","mobile":"'. $mobileresult .'","mobilepref":"'. $mobileprefresult .'","address":"'. $addressresult .'","autofill":"'. $autofillresult .'"}';  

can give me clear explanation of how process entire table rather 1 row? thank much, up-vote useful answers , select best 1 asap! :)

edit: considered bad thing send data client, imagine you'll have 70,000 results sql, gonna send user 15mb of data? dont think :). should learn how use pagination using skip , limit

answer:

well, first let's improve php code

if ($result = $mysqli->query("select id, fullname, mobile, mobilepref, address, autofill users email = '$email';")) {      $userdata = [];     while ($row = $result->fetch_assoc()){         $userdata[] = $row;     }      //no need write json self, php alone     echo json_encode($userdata);      //add mysqli->close()  } 

on swift:

//you did this: let jsondata:nsdictionary = nsjsonserialization.jsonobjectwithdata(urldata!, options:nsjsonreadingoptions.mutablecontainers , error: &error) as! nsdictionary 

and iterate on it

for (k,v) in jsondata {    println("jsondata[\(k)] = \(v)") //a row sql result } 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -