我正在尝试将我的 mysql 数据库连接到我的 xcode 项目,以获得一个简单的登录视图,用户可以在其中输入他们的用户名和密码。我已经设置了数据库,但我需要使用 JSON 还是?
<?php
// Create connection
$con=mysqli_connect("localhost","username","password","dbname");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// This SQL statement selects ALL from the table 'Locations'
$sql = "SELECT * FROM Locations";
// Check if there are results
if ($result = mysqli_query($con, $sql))
{
// If so, then create a results array and a temporary one
// to hold the data
$resultArray = array();
$tempArray = array();
// Loop through each row in the result set
while($row = $result->fetch_object())
{
// Add each row into our results array
$tempArray = $row;
array_push($resultArray, $tempArray);
}
// Finally, encode the array to JSON and output the results
echo json_encode($resultArray);
}
// Close connections
mysqli_close($con);
?>
这是一篇关于这个的好文章:
[1]:http : //codewithchris.com/iphone-app-connect-to-mysql-database/#parsejson Xcode JSON
我没有在 xcode 上花太多时间,但据我所知,没有用于连接到 MySQL 数据库的本机 API/框架(以及大多数其他此类框架)。所以是的 - 您需要使用具有生成 JSON 代码的网页的数据库来设置网站/服务器。然后让 xcode 向服务器/网页发送 Web 请求,将 JSON 代码存储在变量中,然后使用 xcode 必须使用/操作的任何方法来使用/操作 JSON 数据。您需要确保 xcode 和 Web 服务器都不会阻止彼此以适当的安全权限传递数据。
您也可以将数据以任何您想要的格式传递到您的 xcode 应用程序 - JSON、CSV 甚至 XML。无论 xcode 具有什么功能,您都应该坚持使用。真的,你甚至可以组成你自己的数据格式……这是你的应用程序,用它做你想做的事,无论在 xcode 中最容易使用什么。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句