您好,我正在构建一个应用程序,并尝试通过 MySQL 和 PHP 获取数据以在我的应用程序上显示。
我看过一些教程,并试图理解它们,这就是我想到的......
这是我的 php 文件,位于我的服务器 GetData.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");
这是我放在 xcode 中的 ViewController.h 文件中的内容
#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 是一个未使用的变量...请帮助
最佳答案
将 YesOrNo
标签的文本设置为您从网络脚本创建的 strResult
变量:
self.YesOrNo.text = strResult;
看来您很快就进入了应用程序和服务器交互。放慢速度,分别学习 PHP 和 Objectice-C/Cocoa-touch,这样您就可以更轻松地确定要做什么并自行调试。我想说这个问题有点浪费,考虑到应用程序(ios)开发的基本知识,这样的问题通常可以解决
关于php - iOS、PHP 和 mySQL。在 iPhone 上显示数据库中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17517740/