简体   繁体   English

在MySql和Objective C之间传递数据

[英]Passing data between MySql and Objective C

I am working on a small social web application as a final project for my iOS class. 我正在开发一个小型社交网络应用程序,作为我的iOS课程的最终项目。 I have a profile view controller where all the info about the user from the database is supposed to be displayed on the labels. 我有一个配置文件视图控制器,该数据库中有关用户的所有信息都应该显示在标签上。 The problem is that I don't really know the best way to do this. 问题是我真的不知道执行此操作的最佳方法。 Here is my php script: 这是我的PHP脚本:

<?

// Database credentials
$host = 'localhost'; 
$db = 'blabla'; 
$uid = 'blabla'; 
$pwd = 'blabla';

// Connect to the database server   
$link = mysql_connect($host, $uid, $pwd) or die("Could not connect");

//select the json database
mysql_select_db($db) or die("Could not select database");

// Create an array to hold our results
$arr = array();
//Execute the query
$rs = mysql_query("SELECT IdUser, username, fullname, phonenumber, facebook, instagram FROM login");

// Add the rows to the array 
while($obj = mysql_fetch_object($rs)) {
$arr[] = $obj;
}

//return the json result. 
echo '{"users":'.json_encode($arr).'}';
?>

So here I get the info about all the users in the database. 所以在这里,我得到有关数据库中所有用户的信息。 I am sure this is not the right way to go, so I guess I need to change the SQL query to retrieve the data for the current user only. 我确信这不是正确的方法,所以我想我需要更改SQL查询以仅检索当前用户的数据。 But how can I do this? 但是我该怎么办呢? Should I put the username which I enter on the login page into an extra variable and then pass it with JSON to this php script and add the 'WHERE username = 'blabla' statement to the SQL query then? 我是否应该将在登录页面上输入的用户名放入一个额外的变量,然后将其与JSON一起传递给此php脚本,然后将'WHERE username = 'blabla'语句添加到SQL查询中? If so, how can I pass the variable to this script with JSON? 如果是这样,如何使用JSON将变量传递给此脚本?

Can you please give me some sample code? 您能给我一些示例代码吗? Or is there a different way to do this? 还是有其他方法可以做到这一点?

Thank you so much! 非常感谢!

    <?php


// Database credentials
$host = 'localhost'; 
$db = 'blabla'; 
$uid = 'blabla'; 
$pwd = 'blabla';

// Connect to the database server   
$link = mysql_connect($host, $uid, $pwd) or die("Could not connect");

//select the json database
mysql_select_db($db) or die("Could not select database");

//Execute the query
$rs = mysql_query("SELECT IdUser, username, fullname, phonenumber, facebook, instagram FROM login");

// Add the rows to the array 

$data = mysql_fetch_array($rs);

foreach($data as $rec){
    echo "user: $rec<br>";
}

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM