gpt4 book ai didi

mysql - 如何从 mysql 数据库中获取数据并将其显示在 UITableView 中

转载 作者:行者123 更新时间:2023-12-01 00:27:35 27 4
gpt4 key购买 nike

我想制作一个人们必须登录的应用程序,当他们登录时,他们可以从 MYSQL 数据库中获取自己的个人数据,并将该数据显示到 UITableView 中。

最佳答案

    @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 :(";
}
?>

希望对你有帮助:)

关于mysql - 如何从 mysql 数据库中获取数据并将其显示在 UITableView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11704744/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com