
<?PHP/** * Created by PHPStorm. * User: Belal * Date: 12/08/16 * Time: 7:58 PM */define('DB_USERname','root');define('DB_PASSWORD','');define('DB_HOST','localhost');define('DB_name','iphone'); @H_404_11@然后创建另一个PHP文件来创建数据库连接. @H_404_11@ <?PHPclass DbConnect{ private $conn; function __construct() { } /** * Establishing database connection * @return database connection handler */ function connect() { require_once 'Config.PHP'; // Connecting to MysqL database $this->conn = new MysqLi(DB_HOST,DB_USERname,DB_PASSWORD,DB_name); // Check for database connection error if (MysqLi_connect_errno()) { echo "Failed to connect to MysqL: " . MysqLi_connect_error(); } // returing connection resource return $this->conn; }} @H_404_11@现在,您还需要一个文件来处理数据库 *** 作. @H_404_11@ <?PHPclass DbOperation{ private $conn; //Constructor function __construct() { require_once dirname(__file__) . '/Config.PHP'; require_once dirname(__file__) . '/DbConnect.PHP'; // opening db connection $db = new DbConnect(); $this->conn = $db->connect(); } //Function to create a new user public function createTeam($name,$memberCount) { $stmt = $this->conn->prepare("INSERT INTO team(name,member) values(?,?)"); $stmt->bind_param("si",$name,$memberCount); $result = $stmt->execute(); $stmt->close(); if ($result) { return true; } else { return false; } }} @H_404_11@最后,您需要创建将处理您的http请求的PHP文件. @H_404_11@ <?PHP//creating response array$response = array();if($_SERVER['REQUEST_METHOD']=='POST'){ //getting values $teamname = $_POST['name']; $memberCount = $_POST['member']; //including the db operation file require_once '../includes/DbOperation.PHP'; $db = new DbOperation(); //inserting values if($db->createTeam($teamname,$memberCount)){ $response['error']=false; $response['message']='Team added successfully'; }else{ $response['error']=true; $response['message']='Could not add team'; }}else{ $response['error']=true; $response['message']='You are not authorized';}echo Json_encode($response); @H_404_11@现在只需在iOS应用程序上创建视图,然后在buttonclick上向PHP文件发送请求.代码如下. @H_404_11@ //// VIEwController.swift// SwiftPHPMysqL//// Created by Belal Khan on 12/08/16.// copyright © 2016 Belal Khan. All rights reserved.//import UIKitclass VIEwController: UIVIEwController { //URL to our web service let URL_SAVE_TEAM = "http://192.168.1.103/MyWebService/API/createteam.PHP" //TextFIElds declarations @IBOutlet weak var textFIEldname: UITextFIEld! @IBOutlet weak var textFIEldMember: UITextFIEld! //button action method @IBAction func buttonSave(sender: UIbutton) { //created NSURL let requestURL = NSURL(string: URL_SAVE_TEAM) //creating NSMutableURLRequest let request = NSMutableURLRequest(URL: requestURL!) //setting the method to post request.httpMethod = "POST" //getting values from text fIElds let teamname=textFIEldname.text let memberCount = textFIEldMember.text //creating the post parameter by concatenating the keys and values from text fIEld let postParameters = "name="+teamname!+"&member="+memberCount!; //adding the parameters to request body request.httpBody = postParameters.dataUsingEnCoding(NSUTF8StringEnCoding) //creating a task to send the post request let task = NSURLSession.sharedSession().dataTaskWithRequest(request){ data,response,error in if error != nil{ print("error is \(error)") return; } //parsing the response do { //converting resonse to NSDictionary let myJsON = try NSJsONSerialization.JsONObjectWithData(data!,options: .MutableContainers) as? NSDictionary //parsing the Json if let parseJsON = myJsON { //creating a string var msg : String! //getting the Json response msg = parseJsON["message"] as! String? //printing the response print(msg) } } catch { print(error) } } //executing the task task.resume() } overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() // Do any additional setup after loading the vIEw,typically from a nib. } overrIDe func dIDReceiveMemoryWarning() { super.dIDReceiveMemoryWarning() // dispose of any resources that can be recreated. }} @H_404_11@你需要做的另一件事是在Info.pList文件中添加以下行,这是因为默认情况下你不能向非安全URL发送请求,因为我们有http,我们必须做最后的事情. @H_404_11@ <!-- add from here --> <key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> <key>NSExceptionDomains</key> <dict> <key>yourdomain.com</key> <dict> <key>NSIncludesSubdomains</key> <true/> <key>NSThirdPartyExceptionRequiresForwardSecrecy</key> <false/> </dict> </dict> </dict> <!-- end of the code -->@H_404_11@资料来源:iOS MySQL Database Tutorial 总结
以上是内存溢出为你收集整理的ios – 如何使用swift连接mysql?全部内容,希望文章能够帮你解决ios – 如何使用swift连接mysql?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)