dongzhi6087 2013-03-18 20:58
浏览 39
已采纳

iPhone应用程序 - Mysql数据库连接不起作用

I am trying to build an iPhone app that uses data from an online mysql database. (note that programming is new to me)

I have this piece of code in my app, which is supposed to connect to my php file:

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

        }
    }

on my website (hosted by a web hosting company) I have a php file called "login.php" which contains next code:

    <?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


}

?>

These two pieces of code should enable me to access my database, but when I test it ( put a email and password in the table and try to login from my app), the iphone simulator stucks for a while, and after that i tells me that my the entered values are wrong (which means the query returns no value.) In what way do I have to edit these two pieces of code to make it work? Thank you in advance.

  • 写回答

3条回答 默认 最新

  • dongyong6332 2013-03-18 23:08
    关注

    This is just an issue of trial and error. I could put all your code into a project myself and test it, but that is not the purpose of Stack Overflow and I am too lazy.

    Do what every other programmer does: set breakpoints everywhere and/or use NSLog("result: %@",result). Dump your URL with NSLog and try it using your web browser. If there is an error, fix it and try your project again, if not, keep trying.

    Also, I have never seen before this type of language action:@selector(sendLogin); It looks like another copy/paste error.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏