php - How to access my server MySql from IOS simulator to retrieve data swift -
what wrong code, i'm trying mamp sever turned on, have 1 php file in server i'm testing connections , such, this:
<?php header('content-type: application/json'); if($_post) { $username = $_post['username']; $password = $_post['password']; echo $username echo $password if($username && $password) { $db_name = 'dbtest'; $db_user = 'pedro'; $db_password = 'pedro'; $server_url = 'localhost'; $mysqli = new mysqli('localhost', $db_user, $db_password, $db_name); /* check connection */ if (mysqli_connect_errno()) { error_log("connect failed: " . mysqli_connect_error()); echo '{"success":0,"error_message":"' . mysqli_connect_error() . '"}'; } else { if ($stmt = $mysqli->prepare("select username users username = ? , password = ?")) { $password = md5($password); /* bind parameters markers */ $stmt->bind_param("ss", $username, $password); /* execute query */ $stmt->execute(); /* bind result variables */ $stmt->bind_result($id); /* fetch value */ $stmt->fetch(); /* close statement */ $stmt->close(); } /* close connection */ $mysqli->close(); if ($id) { error_log("user $username: password match."); echo '{"success":1}'; } else { error_log("user $username: password doesn't match."); echo '{"success":0,"error_message":"invalid username/password"}'; } } } else { echo '{"success":0,"error_message":"invalid username/password."}'; } }else { echo '{"success":0,"error_message":"invalid data."}'; } ?>
and in xcode app has 3 views in swift, important 1 this:
// // loginviewcontroller.swift // parkingapp // // created pedro alonso on 02/06/15. // copyright (c) 2015 pedro alonso. rights reserved. // import uikit class loginviewcontroller: uiviewcontroller { @iboutlet weak var loginlabel: uilabel! @iboutlet weak var usernamefield: uitextfield! @iboutlet weak var passwordfield: uitextfield! override func viewdidload() { super.viewdidload() // additional setup after loading view. } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } /* // mark: - navigation // in storyboard-based application, want little preparation before navigation override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) { // new view controller using segue.destinationviewcontroller. // pass selected object new view controller. } */ @ibaction func entertapped(sender: uibutton) { var username: string = usernamefield.text var password: string = passwordfield.text if ( username.isempty || password.isempty) { var alertview: uialertview = uialertview() alertview.title = "failed" alertview.message = "error in username or password" alertview.delegate = self alertview.addbuttonwithtitle("ok") alertview.show() } else { var post: string = "username=\(username)&password=\(password)" nslog("post data: %@", post) println(post) var url: nsurl = nsurl(string: "http://localhost:8888/jsonlogin2.php")! var postdata: nsdata = post.datausingencoding(nsasciistringencoding, allowlossyconversion: false)! var postlenght: string = string(postdata.length) var request: nsmutableurlrequest = nsmutableurlrequest(url: url) request.httpmethod = "post" request.httpbody = postdata request.setvalue(postlenght, forhttpheaderfield: "content-length") request.setvalue("application/x-form-urlencoded", forhttpheaderfield: "content-type") request.setvalue("application/json", forhttpheaderfield: "accept") var responseerror: nserror? var response: nsurlresponse? var urldata: nsdata? = nsurlconnection.sendsynchronousrequest(request, returningresponse: &response, error: &responseerror) if urldata != nil { let res = response as! nshttpurlresponse! println(urldata) nslog("response code: %ld", res.statuscode) if (res.statuscode >= 200 && res.statuscode < 300) { var responsedata: nsstring = nsstring(data: urldata!, encoding: nsutf8stringencoding)! nslog("response: ==> %@", responsedata) var error: nserror? let jsondata: nsdictionary = nsjsonserialization.jsonobjectwithdata(urldata!, options: nsjsonreadingoptions.mutablecontainers, error: &error) as! nsdictionary let succes: int = jsondata.valueforkey("succes") as! int if succes == 1 { nslog("login success") var prefs: nsuserdefaults = nsuserdefaults.standarduserdefaults() prefs.setobject(username, forkey: "username") prefs.setinteger(1, forkey: "isloggedin") prefs.synchronize() self.dismissviewcontrolleranimated(true, completion: nil) } else { var errormsg: string? if jsondata["error_message"] as? string != nil { errormsg = jsondata["error_message"] as! string? } else { errormsg = "unknown error" } var alertview: uialertview = uialertview() alertview.title = "sign in failed" alertview.message = errormsg alertview.delegate = self alertview.addbuttonwithtitle("ok") alertview.show() } } else { var alertview:uialertview = uialertview() alertview.title = "sign in failed!" alertview.message = "connection failed" alertview.delegate = self alertview.addbuttonwithtitle("ok") alertview.show() } } else { var alertview:uialertview = uialertview() alertview.title = "sign in failed!" alertview.message = "connection failure" if let error = responseerror { alertview.message = (error.localizeddescription) } alertview.delegate = self alertview.addbuttonwithtitle("ok") alertview.show() } } } }
the .php file in /applications/mamp/htdocs, not clear why giving me response code 500, , i'm @ lost not sure why happening. help?? thanks.
edit: response:
<nshttpurlresponse: 0x7fc42149ac60> { url: http://localhost:8888/jsonlogin2.php } { status code: 500, headers { connection = close; "content-length" = 0; "content-type" = "text/html; charset=utf-8"; date = "thu, 04 jun 2015 12:11:35 gmt"; server = "apache/2.2.29 (unix) mod_wsgi/3.4 python/2.7.8 php/5.6.7 mod_ssl/2.2.29 openssl/0.9.8zd dav/2 mod_fastcgi/2.4.6 mod_perl/2.0.8 perl/v5.20.0"; "x-powered-by" = "php/5.6.7"; } }
i can access safari in simulator localhost:8888, there no connection problem.
edit2: request apparently, because tells me invalid data skipping , returning this:
2015-06-04 17:16:11.914 parkingapp[3777:126598] response: ==> {"success":0,"error_message":"invalid data."}
what wrong way i've done request?
edit2: have changed code , activated mysql log see queries, still $stmt->get_result() or fetch() doing nothing , not know why. i'm not doing throughout ios simple browser here troublesome part.
modified part:
$mysqli = new mysqli('localhost', $db_user, $db_password, $db_name); /* check connection */ if (mysqli_connect_errno()) { error_log("connect failed: " . mysqli_connect_error()); echo '{"success":0,"error_message":"' . mysqli_connect_error() . '"}'; } else { $query = "select dataone,password users username = ? , password = ?"; if ($stmt = $mysqli->prepare($query)) { //$password = md5($password); /* bind parameters markers */ $stmt->bind_param("ss", $username, $password); /* execute query */ $stmt->execute(); //$stmt->debugdumpparams(); echo $stmt->sqlstate; var_dump($stmt); /* bind result variables */ //$stmt->bind_result($dataone,$password); $result = $stmt->get_result(); printf("test: ", $dataone, $password); //fetch value while($stmt->fetch()) { echo $dataone; } /* close statement */ $stmt->close(); } /* close connection */ $mysqli->close(); if ($result != null) { error_log("user $username: password match."); echo '{"success":1, "dataone:"'.$dataone.'}'; } else { error_log("user $username: password doesn't match."); echo '{"success":0,"error_message":"invalid username/password"}'; } }
the $stmt not retuning on get_result() or not going while(fetch()) i'm not know now. help?
well, if web server throwing http error code 500 (internal error) because php script crashing. try , read php log, , try , debugging of php script.
maybe there wrong posted data ios app, making php script fail?
also accessing localhost:8888 safari in case not prove php script working, requires post data script execute. if($_post) {
. browsing script, if statement never true
.
edit:
it times helps verify 1 component @ time. try building simple html form posts username
, password
against server (http://localhost:8888/jsonlogin2.php
). when see working expected, move on making sure app works. way can tell if errors on server (php script) or in app.
it's check $_post
this:
if (!empty($_post)) {}
this check if $_post
is empty.
your app using application/x-form-urlencoded
, guess should be: application/x-www-form-urlencoded
.
but once again. make local html form, , make php script working, , move on app.
Comments
Post a Comment