获取Php和mysql数据库进行通信

时间:2014-10-05 09:13:47

标签: 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];
}
}

不会说谎我确实找到了这个代码eldewhere并且我试图让它工作,所以我可以创建一个登录页面。

2 个答案:

答案 0 :(得分:0)

我认为问题出在你的联系中:

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;
}

答案 1 :(得分:0)

您的 $ DB_HostName 应该是您的主机名/服务器IP地址和端口。 例如,假设您有一个具有IP地址的服务器: 162.2.2.1 ,并且该服务器上的MySql端口是 3306 (如果我不是mysql的默认端口)错了) - 你现在应该写$ DB_HostName = &#39; 162.2.2.1:3306&#39; 这就是它! 如果你有一个主机名,db.php-db.com你可以写它而不是IP地址: $ DB_HostName = &#39; db.php-db.com:3306&#39;

关于安全问题:您可以使用SSL加密来加密您的连接,请在此处阅读更多内容:https://serverfault.com/questions/261134/is-mysql-port-3306-encrypted-and-if-no-how-can-i-encrypt-it

祝你好运,   汤姆。