iOS:与sql数据库通信

标签 ios

如何在不使用 webview 的情况下对 ios 的 phpmyadmin sql 数据库进行读写调用?

最佳答案

除非您的服务器不安全,否则您将无法直接访问您的数据库。相反,您将不得不使用 PHP 作为某种“桥梁”。下面是一些示例代码,可帮助您开始使用 PHP 脚本:

<?PHP
$con = mysql_connect("db.something.com","someuser","somepass");
if (!$con){die('Could not connect: ' . mysql_error());}
mysql_select_db("db", $con);
$result = mysql_query("SELECT * FROM table");
if(mysql_num_rows($result)) {
    while($something = mysql_fetch_assoc($result)) {
      $somethingmore[] = $something;
    }
  }

header('Content-type: application/json');
echo json_encode($somethingmore);
mysql_close($con);
?>

对我来说,我需要它在 JSON 中,所以我将结果格式化为 JSON。在您的应用程序中,只需发出 HTTP 请求即可获取 PHP 脚本的结果。对我来说,就像这样(使用 ASIHTTPRequest 库):

NSURL *url = [NSURL URLWithString:@"http://www.test.com/something.php"];
__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setUsername:@"user"];
[request setPassword:@"pass"];
[request setDownloadProgressDelegate:downloadProgress];
[request setCompletionBlock:^{
    [self dosomething];
}];
[request startAsynchronous];

然后您可以解析数据并适本地做一些事情。

关于iOS:与sql数据库通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5967573/

相关文章:

ios - 如何将 TestFlight 与 Swift 1.2 应用程序一起使用?

iphone - iOS 如何验证应用程序包?

ios - 如何在 Swift 的 didReceiveRemoteNotification 上从 App Delegate 推送 View Controller ?

ios - NewRelicAgent 不适用于 OCTesting

iOS:了解导航推送后何时加载两个 View Controller 的最佳实践

ios - 接收方没有标识符异常的 segue

ios - 由于 json 问题,Restkit 在 iOS 8 中崩溃?

objective-c - Objective-C : How to resolve 'unrecognized selector sent to instance' error

ios - NSURLCache 线程安全吗?

ios - 刷新 iOS 弹出窗口下的表格?