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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11704744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:03:20  来源:igfitidea点击:

How do I get data out of mysql database and show it in a UITableView

mysqlxcodeuitableviewlogin

提问by 2Extreme

I want to make an app where people must login, and when they are logged in that they can get their own personal data out of the MYSQL database, and display that data in to a UITableView.

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

回答by Lollo

    @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

And Now the PHP:

现在是 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 :(";   
}
?>

I hope this will help you :)

我希望这能帮到您 :)