2017-04-13 130 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

相關問題