0

オンライン 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;

        }
    }

私のWebサイト(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


}

?>

これらの 2 つのコードにより、データベースにアクセスできるようになるはずですが、テストすると (テーブルに電子メールとパスワードを入力し、アプリからログインしようとすると)、iPhone シミュレーターがしばらく動かなくなり、その後、私は教えてくれました入力した値が間違っている (つまり、クエリが値を返さないことを意味します)。これら 2 つのコードをどのように編集して機能させる必要がありますか? 前もって感謝します。

4

3 に答える 3

1

これは試行錯誤の問題です。私はあなたのすべてのコードを自分でプロジェクトに入れてテストすることができましたが、それはStack Overflowの目的ではなく、私はあまりにも怠惰です。

他のすべてのプログラマーが行うことを実行します。どこにでもブレークポイントを設定するか、を使用しますNSLog("result: %@",result)。でURLをダンプNSLogし、Webブラウザを使用して試してください。エラーがある場合は修正し、プロジェクトを再試行します。そうでない場合は、引き続き試行します。

また、私はこのタイプの言語をこれまで見たことがありませんaction:@selector(sendLogin);。別のコピー/貼り付けエラーのようです。

于 2013-03-18T23:08:40.507 に答える
1

このコードを試してみましょう NSURL *url = [NSURL URLWithString: [NSString stringWithFormat: @\" http://www.example.com/file.php?data=this \"]]; NSError *エラー; NSString *mydata = [NSString stringWithContentsOfURL:URL エンコーディング:NSASCIIStringEncoding エラー:&error];

于 2013-03-18T21:10:10.890 に答える
0

この行で:

NSString *strURL = [NSString stringWithFormat:@"http://db.imagine-app.nl/login.php?email=%@&password=%@, email.text, password.text];

二重引用符を使用して文字列を閉じるのを忘れました。する必要があります:

NSString *strURL = [NSString stringWithFormat:@"http://db.imagine-app.nl/login.php?email=%@&password=%@", email.text, password.text];
于 2013-03-18T21:02:24.023 に答える