2013-10-01 77 views
3

我不知道如何从Objective-C运行PHP脚本来检索用于查询数据库的GET数据。有没有办法直接从Objective-C执行并从PHP脚本中检索返回的数据?更好的是,是否有功能直接从iOS/Objective-C查询MySQL数据库服务器?任何想法都表示赞赏。iOS/Objective-C - 如何从iPhone应用程序查询Web服务器数据库?

杰克

+0

能你创建的php文件.. – Jitendra

+0

可能的重复[如何发送一个获取请求在iOS?](http://stackoverflow.com/questions/6212453/how-to-send-a-get-request-in-ios ) –

+0

@jake Byman-有你听说过xml和Json ......? –

回答

5

您可以安全地查询mysql数据库,直接从objective-c查询。

,你必须创建一个PHP代码:

<?php 
$con = mysql_connect("server", "username", "password"); 
if (!$con) 
{ 
die('Could not connect: ' . mysql_error()); 
} 

mysql_select_db("username_numberOfTable", $con); 

$query = mysql_query("SELECT id, Name, Type FROM table") 
or die ('Query is invalid: ' . mysql_error()); 

$intNumField = mysql_num_fields($query); 
$resultArray = array(); 

while ($row = mysql_fetch_array($query)) { 

$arrCol = array(); 
for($i=0;$i<$intNumField;$i++) 
    { 

    $arrCol[mysql_field_name($query,$i)] = $row[$i]; 

} 

array_push($resultArray,$arrCol); 

} 

mysql_close($con); 

echo json_encode($resultArray); 

?> 

,这是Objective-C中的一部分:

- (void)viewDidLoad { 

    NSString *stringName = @"Name"; 
    NSString *stringType = @"Type"; 

    NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://yourURL.php?"]]; 

NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 

[receivedData appendData:data]; 

} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 

if (receivedData) { 

     id jsonObjects = [NSJSONSerialization JSONObjectWithData:receivedData options:NSJSONReadingMutableContainers error:nil]; 

    for (NSDictionary *dataDict in jsonObjects) { 

     NSString *stringNameID = [dataDict objectForKey:@"Name"]; 
     NSString *stringTypeID = [dataDict objectForKey:@"Type"]; 


    dict = [NSDictionary dictionaryWithObjectsAndKeys:stringNameID, stringName, stringTypeID, stringType, nil]; 

     [yourNSMutableArray addObject:dict]; 


    } 

    [self.tableView reloadData]; 

} 

} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

NSDictionary *tmpDict = [yourNSMutableArray objectAtIndex:indexPath.row]; 

cell.textLabel.text = [tmpDict objectForKey:stringName]; 
cell.detailTextLabel.text = [tmpDict objectForKey:stringType]; 


} 

,就是这样,我希望我可以帮助

+0

这太棒了!谢谢!其他问题:您认为我应该如何处理在服务器上运行PHP?我必须创建自己的运行PHP的专用服务器吗?你给出了“http://yourURL.php?”的例子,但是实际上该URL如何存在?我是否必须创建一个运行PHP的Web服务器来查询数据库?或者有没有办法在iOS项目目录中拥有该.php文件,以便它可以直接从iOS项目中执行? –

+0

我创建了一个php文件并将它放到服务器上后,我购买了一台服务器和一个数据库mysql。这个php文件,它会使mysql和objective-c之间的连接。实际上“yourURL.php”可能类似于:www.yourname.com/yourfile.php。老实说,我从来没有想过把PHP文件直接放在应用程序中,所以我不能告诉在这种情况下 – Ilario

+0

是啊看到这就是我想知道的。我在务实地考虑购买专用的MySQL服务器仅仅适用于中小型的iOS应用程序会更麻烦。再加上我查询的数据库甚至不是我的!我在外部服务器上托管它,所以它似乎打败了购买将查询另一台服务器的服务器的目的! –

1

有两种类型的服务,可用于将数据发送到Web服务器:

  1. 同步NSURL请求

  2. 异步NSURL请求

如果你只想发布数据到网络服务器,然后使用asynchrono我们的要求,因为它在后台工作,并不会阻止用户界面。

NSString *content = @"txtfiedl.text=1"; 

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.ex.com/yourfile.php"]]; 
[urlRequest setHTTPMethod:@"POST"]; 
[urlRequest setHTTPBody:[content dataUsingEncoding:NSISOLatin1StringEncoding]]; 

[NSURLConnection connectionWithRequest:request delegate:self]; 
相关问题