我想创建一个人们必须登录的应用程序,当他们登录时,他们可以从MYSQL数据库中获取自己的个人数据,并将数据显示在UITableView中。
答案 0 :(得分:6)
@implementation FromDataBase
- (void)viewDidLoad
{
[super viewDidLoad];
//Load Password and Username
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
LoadUsername = [defaults objectForKey:@"TheUsername"];
NSUserDefaults *pass = [NSUserDefaults standardUserDefaults];
LoadPassword = [pass objectForKey:@"ThePassword"];
//Gets the php
FindURL = [NSString stringWithFormat:@"http://localhost/FromDB/DB.php?username=%@",LoadUsername];
// to execute php code
URLData = [NSData dataWithContentsOfURL:[NSURL URLWithString:FindURL]];
// to receive the returend value
DataResult = [[NSString alloc] initWithData:URLData encoding:NSUTF8StringEncoding];
FriendsArray = [DataResult componentsSeparatedByString:@"///"];
NSLog(@"%@", FriendsArray);
SearchFriends = [[NSMutableArray alloc] initWithArray:FriendsArray];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [SearchFriends count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
cell.textLabel.text = [SearchFriends objectAtIndex:indexPath.row];
return cell;
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
TheSellected = [SearchFriends objectAtIndex:indexPath.row];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:TheSellected
message:@""
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alert show];
return;
}
@end
现在PHP:
<?php
if (isset ($_GET["username"]))
$username = $_GET["username"];
else{
$username = "";
}
//DateBase Infomation
$DB_HostName = "localhost";
$DB_Name = "Users";
$DB_User = "root";
$DB_Pass = "";
$DB_Table = Friends;
//Connecting To Database
$con = mysql_connect($DB_HostName,$DB_User,$DB_Pass);
mysql_select_db($DB_Name,$con);
if(!$con)
{
die('Connection Failed'.mysql_error());
}
$result = mysql_query("select * from $DB_Table ");
$numrows = mysql_num_rows($result);
if ($numrows!=0){
while ($row = mysql_fetch_assoc ($result)){
$Friends = $row['Friends'];
echo "$Friends///";
}
}else{
echo "FAILED :(";
}
?>
我希望这会对你有所帮助:)。