我正在尝试构建一个使用来自在线 mysql 数据库的数据的 iPhone 应用程序。(请注意,编程对我来说是新的)
我的应用程序中有这段代码,它应该连接到我的 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;
}
}
在我的网站(由网络托管公司托管)上,我有一个名为“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 模拟器卡住了一段时间,然后我告诉我我输入的值是错误的(这意味着查询没有返回值。)我必须以什么方式编辑这两段代码才能使其工作?先感谢您。