您好,我正在構建一個應用程序,並試圖通過MySQL和PHP獲取數據以顯示在我的應用程序中。iOS,PHP和mySQL。顯示來自Iphone上的數據庫的數據
我已經看了一些教程,我試圖讓他們的感覺,這就是我想出了...
這是位於我的服務器上訪問getdata.php
我的PHP文件$mysqli = new mysqli("myhostingserver", "username", "password", "database");
$result = $mysqli->query("SELECT id, CASE open when 1 then 'Yes' when 0 then 'No' END as open, date, time, extra1, extra2, lastUpdated FROM MyTable");
,這就是我把我的ViewController.h文件在Xcode
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (retain, nonatomic) IBOutlet UILabel *YesOrNo;
@end
,這是從我ViewController.m代碼
#import "ViewController.h"
@interface ViewController()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *strURL = [NSString stringWithFormat:@"http://myhostingserver.com/GetData.php"];
// to execute php code
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
// to receive the returend value
NSString *strResult = [[[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding]autorelease];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
[_YesOrNo release];
[super dealloc];
}
@end
我認爲這看起來是正確的,但我如何獲得在文本框中打開的數據?
它會沿*YesOrNow = *strResult['open']
?
我跑我上面的代碼中,沒有錯誤只是一個警告,說* strResult是一個未使用的變量應用......請幫助
謝謝你的工作:) – user2499454
首先,我不得不我的PHP文件回聲的價值,然後我申請了'self.YesOrNo.text = strResult;'它工作!我的問題是我打算有多個標籤,我想要顯示更多的數據從sql select語句'$ result = $ mysqli-> query(「SELECT id,CASE open when 1 then'Yes'when 0 then '否'結束爲開放,日期,時間,extra1,extra2,lastUpdated FROM MyTable「);' – user2499454
是否有需要從sql語句中選擇哪些數據,如'self.YesOrNo.text = strResult ['open'] '或'self.YesOrNo.text = strResult ['lastUpdated']'或者我將不得不爲每個要顯示的數據編寫一個新的php文件? – user2499454