MySQL 到 PHP 到 XCode?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8907287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:01:54  来源:igfitidea点击:

MySQL to PHP to XCode?

phpiphonemysqlxcode

提问by Jasmine

I would like to clear my doubts in hooking up of MySQL database with XCode. My application would need to retrieve data from MySQL as there would be a login screen. As such, in order for me to retrieve data from my database, there is a need for me to create a database using MySQL and connect it using PHP and then connect PHP to XCode?

我想消除我对使用 XCode 连接 MySQL 数据库的疑虑。我的应用程序需要从 MySQL 检索数据,因为会有一个登录屏幕。因此,为了让我从我的数据库中检索数据,我是否需要使用 MySQL 创建一个数据库并使用 PHP 连接它,然后将 PHP 连接到 XCode?

I am a greenhorn in application developing but I am tasked to do it for my school.

我是应用程序开发的新手,但我的任务是为我的学校做这件事。

I would need great help in creating a PHP in connecting MySQL(it would be good if its steps-by-steps guide). I would really truly appreciate your kind generous reply.

我需要很大的帮助来创建连接 MySQL 的 PHP(如果它的分步指南会很好)。我真的很感激你的慷慨答复。

Thank you in advance!

先感谢您!

回答by Ilion

It is very simple to connect to a MySQL database with PHP. There are a couple of APIs for this, mysql and mysqli. Mysqli is probably the better one to use, but mildly denser. The Mysql one works like this:

使用 PHP 连接到 MySQL 数据库非常简单。为此有几个 API,mysql 和 mysqli。Mysqli 可能是更好用的一种,但稍微密集一些。Mysql 的工作方式是这样的:

$db = mysql_connect("host:port", "username", "paswword");

mysql_select_db("my_db", $db);

# say we want to select everything from the table Persons
$result = mysql_query("SELECT * FROM Persons");

while ($row = mysql_fetch_array($result))
{
   # do your magic
   # columns are accessed in a zero based array
   # such as $row[0], $row[1], etc. 
   # look at mysql_fetch_assoc to see how to access
   # using the column names
}

mysql_close($db);

There's what looks like an older but still valid W3c tutorial hereand the MySQL PHP API referencethere. To learn about the API differences read the Overview of the MySQL PHP drivers.

这里有一个看起来更旧但仍然有效的W3c 教程,那里有MySQL PHP API 参考。要了解 API 差异,请阅读MySQL PHP 驱动程序概述

As the other answers have stated you'll want the PHP to output something like JSON or XML to communicate with your app and the XCode.

正如其他答案所述,您希望 PHP 输出 JSON 或 XML 之类的内容,以便与您的应用程序和 XCode 进行通信。

回答by unsunghero

This tutorial follows the whole process through step by step from creating a web service to implementing the web service in your app. I found it super easy to follow.

本教程逐步介绍了从创建 Web 服务到在您的应用程序中实现 Web 服务的整个过程。我发现它非常容易遵循。

Part 1: http://www.raywenderlich.com/2941/how-to-write-a-simple-phpmysql-web-service-for-an-ios-app

第 1 部分:http: //www.raywenderlich.com/2941/how-to-write-a-simple-phpmysql-web-service-for-an-ios-app

Part 2: http://www.raywenderlich.com/2965/how-to-write-an-ios-app-that-uses-a-web-service

第 2 部分:http: //www.raywenderlich.com/2965/how-to-write-an-ios-app-that-uses-a-web-service

回答by TJHeuvel

It sounds like you need some sort of WebService. What you can do is just create your PHP pages and let them output a set format (say JSON or XML). Then in your Obj-C application just do the webrequests and parse the results.

听起来您需要某种 WebService。您可以做的只是创建您的 PHP 页面并让它们输出设置的格式(例如 JSON 或 XML)。然后在您的 Obj-C 应用程序中执行 webrequests 并解析结果。

There might be some existing solutions which you can use, Webservice is the keyword here.

可能有一些现有的解决方案可以使用,Webservice 是这里的关键字。

回答by TJHeuvel

Here what sounds better to connect to a mysql database, your best bet is to use JSON/SOAP/XML/PHPwebsevicesto talk between your database and your app..

在这里,连接到 mysql 数据库听起来更好,你最好的选择是用来JSON/SOAP/XML/PHPwebsevices在你的数据库和你的应用程序之间进行对话。

The reason database connection directly from the device is a bad idea, is that you have to enable global external access to it for it to work. You can keep your data safer by having scripts on your server do the communication to the database.

直接从设备连接数据库是一个坏主意的原因是,您必须启用对它的全局外部访问才能使其工作。通过让服务器上的脚本与数据库进行通信,您可以使数据更安全。

One example of how to do this is create PHP pages that export XML data as your mysql data export , and use GET POST methods to post data to PHP pages to write to your database..

如何做到这一点的一个例子是创建 PHP 页面,将 XML 数据导出为您的 mysql 数据导出,并使用 GET POST 方法将数据发布到 PHP 页面以写入您的数据库。