2017-04-13 67 views
-2

我想连接我的mysql数据库到我的xcode项目,用户可以在其中输入用户名和密码的简单登录视图。我已经建立了数据库,但我需要使用JSON或?我想连接我的mySQL数据库到我的xcode项目

<?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

好像你留下的问题不完整。 –

回答

1

这里是一个很好的文章:

[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中最简单的工作。

+0

谢谢!这有很大帮助。 – ElweTheFeared

相关问题