简体   繁体   中英

Passing data between MySql and Objective C

I am working on a small social web application as a final project for my iOS class. 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:

<?

// 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. 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? If so, how can I pass the variable to this script with 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>";
}

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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