php - 让 Php 和 mysql 数据库进行通信

标签 php mysql vps

我正在使用 php 让我的 xcode 应用程序读取数据并将数据写入我的 mysql 数据库。这是我的代码

<?php
 if (isset($_GET["userName"])  && isset($_GET["password"]) ){
            $userName = $_GET["userName"];
            $password = $_GET["password"];
            $result = login( $userName, $password);
            echo $result;
            }

function makeSqlConnection()
{
 $DB_HostName = "what do i put here?";
$DB_Name = "i know this";
$DB_User = "and this";
$DB_Pass = "And this";

$con = mysql_connect($DB_HostName,$DB_User,$DB_Pass) or die(mysql_error()); 

    mysql_select_db($DB_Name,$con) or die(mysql_error()); 

return $con;
}

function disconnectSqlConnection($con)
{
mysql_close($con);
}

function login($userName, $password)
{

$con = makeSqlConnection();

$sql = "select * from user  where userName = '$userName' and password = '$password';";
$res = mysql_query($sql,$con) or die(mysql_error());

$res1 = mysql_num_rows($res);

disconnectSqlConnection($con);

 if ($res1 != 0) {
    return 1;
}else{
    return 0;
}


} 

?>

我还需要做些什么来确保它的安全。感谢您提供的任何帮助。 我也在我的 vps 上运行这个。

xcode 中的人员发布我正在使用此代码的数据:

- (IBAction)continueClicked:(id)sender {
NSInteger success = 0;
@try {

    if([[self.txtUsername text] isEqualToString:@""] || [[self.txtPassword text] isEqualToString:@""] ) {

        [self alertStatus:@"Please enter Email and Password" :@"Sign in Failed!" :0];

    } else {
        NSString *post =[[NSString alloc] initWithFormat:@"userName=%@&passWord=%@",[self.txtUsername text],[self.txtPassword text]];
        NSLog(@"PostData: %@",post);

        NSURL *url=[NSURL URLWithString:@"(my php url)"];

        NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

        NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];

        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
        [request setURL:url];
        [request setHTTPMethod:@"POST"];
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:postData];

        //[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];

        NSError *error = [[NSError alloc] init];
        NSHTTPURLResponse *response = nil;
        NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

        NSLog(@"Response code: %ld", (long)[response statusCode]);

        if ([response statusCode] >= 200 && [response statusCode] < 300)
        {
            NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
            NSLog(@"Response ==> %@", responseData);

            NSError *error = nil;
            NSDictionary *jsonData = [NSJSONSerialization
                                      JSONObjectWithData:urlData
                                      options:NSJSONReadingMutableContainers
                                      error:&error];

            success = [jsonData[@"success"] integerValue];
            NSLog(@"Success: %ld",(long)success);

            if(success == 1)
            {
                NSLog(@"Login SUCCESS");
            } else {

                NSString *error_msg = (NSString *) jsonData[@"error_message"];
                [self alertStatus:error_msg :@"Sign in Failed!" :0];
            }

        } else {
            //if (error) NSLog(@"Error: %@", error);
            [self alertStatus:@"Connection Failed" :@"Sign in Failed!" :0];
        }
    }
}
@catch (NSException * e) {
    NSLog(@"Exception: %@", e);
    [self alertStatus:@"Sign in Failed." :@"Error!" :0];
}
if (success) {
    [self performSegueWithIdentifier:@"login_success" sender:self];
}
}

不会撒谎,我确实在其他地方找到了这段代码,并且正在尝试让它工作,这样我就可以创建一个登录页面。

最佳答案

我相信问题出在您的连接上:

function makeSqlConnection()
{
    $DB_HostName = "localhost";
    $DB_User = "your_database_user";
    $DB_Pass = "your_database_user_password";
    $DB_Name = "your_database_name";

    $con = mysql_connect( $DB_HostName,$DB_User,$DB_Pass, $DB_Name ) or die( mysql_error() ); 

    return $con;
}

关于php - 让 Php 和 mysql 数据库进行通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26201240/

相关文章:

php - WooCommerce 电子邮件通知中基于产品类别的不同收件人

java - 使用 Hibernate 和 MySQL 在运行时自动提交更改

ssh - Gitlab 指纹已经被占用,部署 key 项目部署 key 指纹已经被占用

php - 从 doc 和 docx 中提取文本

python - Gtk警告: could not open display

xml - 如何从产品页面中删除 'tags' block ?

php - 如何将页面已显示的数据导出为pdf/excel?

php - 如何在Excel中获取MySQL表实体

java - 填充数据库中的外键问题

java - java中MySqlDB中的图像添加水印