[英]Communicate with MySQL Database with PHP in a jQuery Mobile Application
[英]Getting Php and mysql database to communicate
我正在使用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;
}
$ DB_HostName應該是您的主機名/服務器IP地址和端口。 例如,假設您有一台IP地址為162.2.2.1的服務器,並且該服務器上的MySql端口為3306 (如果我沒有記錯的話,默認的mysql端口)-現在應該寫$ DB_HostName = '162.2.2.1 :3306'就是這樣! 如果您有一個主機名db.php-db.com,則可以寫一個主機名而不是IP地址:$ DB_HostName = 'db.php-db.com : 3306'
關於安全性問題:您可以使用SSL加密對連接進行加密,請在此處了解更多信息: https : //serverfault.com/questions/261134/is-mysql-port-3306-encrypted-and-if-no-how-can -i-encrypt-it
湯姆,祝你好運。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.