iOS:与sql数据库通信 [英] iOS: communicating with sql database

查看:111
本文介绍了iOS:与sql数据库通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

how do I make read and write calls to a phpadmin sql database for ios without using webview?

推荐答案

除非你的服务器如果不安全,您将无法直接访问您的数据库。相反,你必须使用PHP作为一种桥梁。这里有一些示例代码可以帮助您开始使用PHP脚本:

Unless your server is insecure, you won't be able to access your database directly. Instead, you'll have to use PHP as a "bridge", of sorts. Here's some sample code to get you started on your PHP script:

<?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库):

For me, I needed it in JSON so I formatted the result as JSON. In your application, just make an HTTP request to get the result of the PHP script. For me, it was like this (Using the ASIHTTPRequest library):

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];

然后你可以解析数据并做适当的事情。

Then you can parse the data and do stuff appropriately.

这篇关于iOS:与sql数据库通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆