我不知道如何從Objective-C運行PHP腳本來檢索用於查詢數據庫的GET數據。有沒有辦法直接從Objective-C執行並從PHP腳本中檢索返回的數據?更好的是,是否有功能直接從iOS/Objective-C查詢MySQL數據庫服務器?任何想法都表示讚賞。iOS/Objective-C - 如何從iPhone應用程序查詢Web服務器數據庫?
傑克
我不知道如何從Objective-C運行PHP腳本來檢索用於查詢數據庫的GET數據。有沒有辦法直接從Objective-C執行並從PHP腳本中檢索返回的數據?更好的是,是否有功能直接從iOS/Objective-C查詢MySQL數據庫服務器?任何想法都表示讚賞。iOS/Objective-C - 如何從iPhone應用程序查詢Web服務器數據庫?
傑克
您可以安全地查詢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];
}
,就是這樣,我希望我可以幫助
這太棒了!謝謝!其他問題:您認爲我應該如何處理在服務器上運行PHP?我必須創建自己的運行PHP的專用服務器嗎?你給出了「http://yourURL.php?」的例子,但是實際上該URL如何存在?我是否必須創建一個運行PHP的Web服務器來查詢數據庫?或者有沒有辦法在iOS項目目錄中擁有該.php文件,以便它可以直接從iOS項目中執行? –
我創建了一個php文件並將它放到服務器上後,我購買了一臺服務器和一個數據庫mysql。這個php文件,它會使mysql和objective-c之間的連接。實際上「yourURL.php」可能類似於:www.yourname.com/yourfile.php。老實說,我從來沒有想過把PHP文件直接放在應用程序中,所以我不能告訴在這種情況下 – Ilario
是啊看到這就是我想知道的。我在務實地考慮購買專用的MySQL服務器僅僅適用於中小型的iOS應用程序會更麻煩。再加上我查詢的數據庫甚至不是我的!我在外部服務器上託管它,所以它似乎打敗了購買將查詢另一臺服務器的服務器的目的! –
有兩種類型的服務,可用於將數據發送到Web服務器:
同步NSURL請求
異步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];
能你創建的php文件.. – Jitendra
可能的重複[如何發送一個獲取請求在iOS?](http://stackoverflow.com/questions/6212453/how-to-send-a-get-request-in-ios ) –
@jake Byman-有你聽說過xml和Json ......? –