简体   繁体   中英

how to access mysql from iphone

我是iphone的初学者。我想为我的应用程序创建一个登录页面。我无法弄清楚如何连接到php页面,并从mysql数据库到iphone检索相应的数据。它。

what does the iphone have to do with a connection between php and mysql ?

PHP will run with on a web server probably apache installed on some computer and it will connect to a MySQL db .. and u will access that php page from your iphone with a browser. Not sure what part will the iphone have in all this other than providing the browser

You might want to have a look at NSURLRequest which you can use with a NSURLConnection to send eg GET-Parameters to a URL. You can then implment the NSURLConnectionDelegate to respond to incoming data:

1) setup connection

receivedData =[NSMutableData data];
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:url] 
                                          cachePolicy:NSURLRequestUseProtocolCachePolicy
                                      timeoutInterval:20.0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

2) Setup delegate methods in self:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response {
    NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *) response;  

    if([httpResponse statusCode]==200)
       [receivedData setLength:0];
    else
       NSLog(@"Http-Reponse %u",[httpResponse statusCode]);
       // HANDLE ERROR!
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
   // append the new data to the receivedData
   // receivedData is declared as a method instance elsewhere
   [receivedData appendData:data];
}



- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
   // HANDLE THE CONNECTION ERROR
   // release the connection, and the data object
   [connection release];
   // receivedData is declared as a method instance elsewhere
   [receivedData release];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
     // receivedData contains the data
     // convert to string:
     NSLog(@"finished loading: %@",[[[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding] autorelease]);
     [connection release];
     [receivedData release];
}

You'll want to expose the authentication functionality as a web service, then use the URL Loading code posted by Felix L. to initiate an actual connection to the web service.

You'll probably want to send a response from the server as XML, if so, you'll parse that response with an NSXMLParser, otherwise you can just send the response in whatever format you'd like and parse it appropriately.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM