iOS開發與PHP後台開發的互動

來源:互聯網
上載者:User

標籤:

<?php            /*     建立php檔案的方法     1):將XAMPP的存放路徑找到 然後在htdocs檔案下建立新檔案夾      使用電腦的文字編輯器 建立一個檔案儲存都案頭 然後顯示簡介將尾碼名改為 .php     最後間這個檔案放入剛開始建立好的檔案夾中     然後就可在裡面編寫php代碼了     2):iOS和PHP通訊協議的設計和互動     iOS發送網路請求: 非同步和同步 GET POST         //此部分是OC代碼部分 放這裡方便一起查看     /* GET     //註:網址的前半部分分別是:伺服器位址/存放php檔案夾得名字/php檔案的名字     後半部分:都是欄位  欄位和欄位之間用 & 符號連結          建立兩個輸入框聲明成屬性 然後將這兩個輸入框的 text 存放進建立的資料庫中          NSURLSession *session = [NSURLSession sharedSession];     NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://127.0.0.1/phpTest/aviTest.php?name=%@&password=%@",_nameTF.text,_passwordTF.text]];     NSURLSessionTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {     NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];          //        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];          NSLog(@"result = %@",result);               }];     [task resume];     */        /*          NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/phpTest/sssss.php"];          NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];          request.HTTPMethod = @"POST";     request.HTTPBody = [[NSString stringWithFormat:@"name=%@&password=%@",_nameTF.text,_passwordTF.text] dataUsingEncoding:NSUTF8StringEncoding];          NSURLSession *session = [NSURLSession sharedSession];          NSURLSessionTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {          NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];     NSLog(@"%@", result);          NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error:nil];     NSLog(@"result:%@",dic[@"result"]);          }];          [task resume];          */              //這是PHP代碼部分        //request 請求既可以執行 POST 也可以執行 GET 請求    //$data = $_GET;    //$data = $_POST;        $data = $_REQUEST;        $name = $data["name"];    $password = $data["password"];    $array = array();    //登入伺服器資料庫 第一個參數:資料庫伺服器地址,第二個參數:伺服器使用者名稱;第三個參數:密碼    $connect = mysql_connect("127.0.0.1","root","");    if($connect){        // echo(‘串連伺服器成功‘);                $array["connecty"] = "success";    }else{        echo("串連失敗");        die;        $array["connect"] = "error";    }        //選擇資料庫    mysql_select_db("Person",$connect);        //將使用者名稱、密碼插入到資料庫中    if(mysql_query("INSERT INTO register(name, password) VALUES (‘$name‘,‘$password‘)")){        //echo("插入成功");        $array["result"] = "success";    }else{        //echo("插入失敗");        $array["result"] = "error";    }    echo(json_encode($array));        ?>

 

iOS開發與PHP後台開發的互動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.