以下是用PHP访问数据库的代码

 1 <?php
 2 
 3 class itcastUsers {
 4         private $db;
 5 
 6         // 构造函数-建立数据库链接
 7         function __construct() {
 8                 $this->db = new mysqli('127.0.0.1', 'root', '123456', 'itcast');
 9 
10                 if (mysqli_connect_errno()){
11                         printf("连接错误:%s\n", mysqli_connect_error());
12                         exit();
13                 }
14                 
15                 $this->db->autocommit(FALSE);
16         }
17         
18         // 析构函数-关闭数据库连接
19         function __destruct() {
20                 $this->db->close();
21         }        
22 
23         // 用户登录
24         function userLogin() {
25                 if (isset($_GET['username']) && isset($_GET['password'])){
26                         // 获取GET请求参数
27                         $accessType = '[GET]';
28                         $userName = $_GET['username'];
29                         $userPassword = $_GET['password'];
30                 } else if (isset($_POST['username']) && isset($_POST['password'])){
31                         // 获取POST请求参数
32                         $accessType = '[POST]';
33                         $userName = $_POST['username'];
34                         $userPassword = $_POST['password'];
35                 } else {
36                         echo('非法请求!');
37                         return false;
38                 }
39 
40                 // 设置数据库查询字符编码
41                 $this->db->query('set names utf8');
42                 // 查询请求
43                 $data = $this->db->query("SELECT id, userName, userImage, FROM userInfo WHERE userName='$userName' AND userPwd='$userPassword'");
44                 // 绑定查询参数
45                 $this->db->real_escape_string($userName);
46                 $this->db->real_escape_string($userPassword);
47                 // 提交查询请求
48                 $this->db->commit();
49                 // 提取一条查询结果
50                 $row = $data->fetch_assoc();
51                 // 将结果绑定到数据字典
52                 $result = [
53                         'userId' => $row['id'],
54                         'userName' => $row['userName'],
55                         'userImage' => $row['userImage']
56                         ];
57                 // 将数据字典使用JSON编码
58                 echo json_encode($result);
59                 return true;
60         }
61 }
62 
63 header('Content-Type:text/html;charset=utf-8');
64 $itcast = new itcastUsers;
65 $itcast->userLogin();
66 
67 ?>

 

通过Get访问数据库的代码:

 1 -(void)getLogin{
 2     NSString *urlStr =[NSString stringWithFormat:@"http://localhost/login.php?username=%@&password=%@",self.userName.text,self.userPassword.text];
 3     
 4     NSURL *url = [NSURL URLWithString:urlStr];
 5     
 6     NSURLRequest *request = [NSURLRequest requestWithURL:url];
 7     
 8     [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
 9         
10         NSString *str = @"misaka";
11         str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
12         [[NSThread currentThread] setName:@"HHHH"];
13         NSLog(@"-----%@ ~~~~~~~~ %@--------",str,[NSThread currentThread]);
14     }];
15     NSLog(@"come here %@",[NSThread currentThread]);
16 }

通过Post访问数据库的代码:

 1 - (void)postLogin{
 2     // 1.url
 3     NSString *urlStr = [NSString stringWithFormat:@"http://127.0.0.1/login.php"];
 4     NSURL *url = [NSURL URLWithString:urlStr];
 5     // 2.request
 6     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
 7     request.HTTPMethod = @"POST";
 8     NSString *str = [NSString stringWithFormat:@"username=%@&password=%@", self.userName.text, self.userPassword.text];
 9     request.HTTPBody = [str dataUsingEncoding:NSUTF8StringEncoding];
10     // 3.connection(网络请求统一使用异步方式)
11     [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
12         
13         if (connectionError == nil) {
14             // 网络请求结束以后
15             // 将data转字符串
16             NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
17             NSLog(@"str:%@,%@", data, [NSThread currentThread]);     // num:2 or 3
18             
19             // 更新ui(统一在主线程)
20             [[NSOperationQueue mainQueue] addOperationWithBlock:^{
21                 NSLog(@"更新ui %@", [NSThread currentThread]);                                 // num:1
22                 self.resultLable.text = @"POST";
23             }];
24         } else {
25             NSLog(@"%@", connectionError.localizedDescription);
26         }
27         
28     }];
29     
30     NSLog(@"come here! %@", [NSThread currentThread]);    // num:1
31 }