我試圖建立一個從在線MySQL數據庫使用數據的iPhone應用程序。 (請注意,編程是新的我)iPhone應用程序 - Mysql數據庫連接不工作
我有這樣一段代碼在我的應用程序,這是應該連接到我的PHP文件:
//loginAction
- (void) loginAction
{
if ([email.text isEqualToString:@""] || [password.text isEqualToString:@""]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"please fill in everything":self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
return;
}
//database connection
NSString *strURL = [NSString stringWithFormat:@"http://db.imagine-app.nl/login.php?email=%@&password=%@", email.text, password.text];
//execute php
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
//receive data
NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
if ([strResult isEqualToString:@"1"])
{
action:@selector(sendLogin);
}else
{
//invalid information
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"E-mail or password wrong" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
return;
}
}
在我的網站(由Web託管公司託管)我有一個名爲「login.php中」一個php文件,其中包含下面的代碼:
<?php
if (isset($_GET["email"]) && isset($_GET["password"])){
$email = $_GET["email"];
$password = $_GET["password"];
$result = login($email, $password);
echo $result;
}
function makeSqlConnection()
{
$DB_HostName = "db.imagine-app.nl";
$DB_Name = "*************";
$DB_User = "*************";
$DB_Pass = "*************";
$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($email, $password)
{
$con = makeSqlConnection();
$sql = "SELECT * from user WHERE email = '$email' 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;
}// end else
}
?>
代碼這兩件應讓我來訪問我的數據庫,但是當我測試它(把電子郵件和密碼在表,並嘗試從我的應用程序登錄),iPhone模擬器stuses一段時間,之後,我告訴米Ë我輸入的值是錯誤的(這意味着該查詢返回值。) 以何種方式做我必須編輯這些兩段代碼,使其工作? 預先感謝您。
1st close the'''here:'NSString * strURL = [NSString stringWithFormat:@「http://db.imagine-app.nl/login.php?email=%@&password=%@」,email。文本,password.text];' – 2013-03-18 21:01:19