通过PHP访问SQL数据库? [英] Access SQL database through PHP?

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

问题描述

我是PHP&的新开发人员. SQL场景.到目前为止,我只完成了Objective-C编程.但是,我的一个项目要求我拥有一个在线数据库,我需要从我的应用程序访问该数据库.我将使用MCPKit框架来像这样远程访问我的数据库:

I'm a spanking new developer to the PHP & SQL scene. I've only done Objective-C programming so far. However one of my projects requires me to have an online database which I need to access from my application. I was going to use the MCPKit framework to remotely access my DB like this:

MCPConnection *db;
db = [[MCPConnection alloc] initToHost:@"db.something.com" withLogin:@"someuser" usingPort:3306];
[db setPassword:@"somepass"];
NSLog(@"Connect: %d", [db connect]);

但是这不起作用,因为我联系了我的托管服务提供商,并且他们已经对其进行了设置,因此我不能从外部主机访问我的数据库(出于安全性考虑).因此,我将不得不寻找替代方案.我唯一想到的就是在服务器上设置一个PHP脚本,该脚本将下载整个数据库并将其作为.sql文件提供给我,然后我可以对其进行操作.

But this doesn't work because I contacted my hosting provider and they have it set up so that I can't access my database from an external host (for security). So, I'll have to look for alternatives. The only thing I could think of would be to set up a PHP script that would be on my server that would download the entire database and feed it to me as a .sql file which I can then manipulate.

但是,我不知道从哪里开始.我发现您可以像这样在PHP中访问数据库:

However, I have no clue where to start here. I found that you can access a database in PHP like this:

$mysql = new MySQLi('db.something.com', 'someuser', 'somepass', 'mydb')
$mysql->query("SELECT * FROM `something`");

但是我还没有尝试过,我也不知道如何获得结果.

However I haven't tried this and I don't know how I can access the result of this.

要解决这个问题,我想知道如何访问远程数据库,并让PHP脚本将数据库作为文件发送给我,我可以在我的Cocoa应用程序中对其进行操作.

To boil the question down, I want to know how to access a remote database and have a PHP script send me the database as a file which I can manipulate in my Cocoa application.

实际上,如果可以在Cocoa应用程序中 内运行PHP脚本,那就更好了.想法?

In fact, if running the PHP script can be done inside the Cocoa app it'd be even more awesome. Ideas?

好吧,我最终是这样做的:

Well, I ended up doing it like this:

<?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($device = mysql_fetch_assoc($result)) {
      $devices[] = $device;
    }
  }

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

一旦运行,我就可以对生成的JSON进行处理.这是个好方法吗?

Once that runs I can do stuff with the resulting JSON. Is this a good way to do it?

推荐答案

由于性能(和安全性)方面的原因,下载整个数据库并进行更改后上载可能没有意义.

Downloading the entire database and uploading it with the changes will probably not make sense for performance (and security) reasons.

通常要做的是使用可以从iOS应用程序调用的PHP或其他服务器端语言设置API,然后为您执行操作.

What one would usually do is set up an API in PHP or some other server side language that can be called from your iOS app, and does the operations for you.

我对它们没有经验,因此无法判断它们的完整性和健壮性,但是有现成的PHP API可以将mySQL操作转换为REST操作,例如

I have no own experience with them so I can't judge on their completeness and robustness, but there are ready-made PHP APIs that translate mySQL operations into REST operations like phprestsql. Maybe that's something for you; however note that you always need to solve the problem of authentication. If you have an API with write access (or read access to sensitive materials), you will need to do something to prevent everyone on the Internet from accessing that URL.

编辑:如果仅用于轮询已定义的数据集,则可以使用标准的PHP查询表并在循环中输出内容"脚本(最好使用PDO)来完成,并返回 json_encode() d结果.

if it's just about polling a defined set of data, you may be easiest off with a standard PHP "query a table and output stuff in a loop" script (preferably using PDO), and returning a json_encode()d result.

这篇关于通过PHP访问SQL数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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