如何在不使用 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/