phonegap / msql(i)无法建立与数据库的连接

时间:2013-07-02 15:55:18

标签: mysql ios phpmyadmin cordova database-connection

我目前正在开发适用于iOS的网络应用,但应用无法正确连接。

当我尝试登录时,我会收到以下iOS弹出窗口:index.html错误。

当PHP文件在线时,

这里是我的一些代码:

connection.php

<?php //Make connection with database
// Verbinden met MySQL Database
$host = "localhost"; // Welke server : localhost 
$username = "*******"; // Gebruikersnaam
$password = "****"; // Wachtwoord
$dbnaam = "*****"; // Naam van de database
$db_error1 = "<p>FOUT: verbinden met databaseserver is mislukt</p>"; // Foutmelding 1
$db_error2 = "<p>FOUT: selecteren van database is mislukt</p>"; // Foutmelding 2
$db_error3 = "<p>FOUT: sluiten van database is mislukt</p>"; // Foutmelding 3

// Verbinden met Databaseserver
$con=mysqli_connect($host, $username, $password, $dbnaam);// or die($db_error1);
// verbinden met de database
//mysql_select_db($dbnaam, $db) or die($db_error2);
if (mysqli_connect_errno($con))
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
?>

demo.js

// define the URL of the server component
//var url ="http://www.jorisgraaumans.nl/dutchmobile/";
//var url = "http://localhost:8888/Mobile/Festival/";
var url = "http://www.rikvandoorn.com/mobile/";

// afvangen van het standaard submit event
// zie ook: http://api.jquery.com/submit/

$('#login').on('pageinit', function(event) {
    $('#loginForm').submit(function() {
        $.ajax({
            type: "POST",
            url: "processLogin.php",
            cache: "false",
            dataType: "json",
            data: {
                email : $('#email').val(),
                wachtwoord : $('#wachtwoord').val(),
            },
            success: function(phpData){ 
                $("#return").data("login", phpData.login);
                console.log("login is: "+ phpData.login);
                if(phpData['error'] == true){
                    $.mobile.changePage("#return", {
                        transition : "fade"
                    })
                } else {
                    $.mobile.changePage("#home", {
                        transition : "fade"
                    })
                }
            },  
            error: function(){
                    alert('Error');
            }                   
        }); 
        return false; // return false to prevent the default submit of the form to the server.
    });
    // end: pageinit loginForm  
});
$('#return').on('pageshow', function(event) {
    $("#error_message_log").empty();
    $("#error_message_log").prepend('<p>' +  $(this).data('login') + '</p>');
});

processLogin.php

<?php //process login form 

include 'http://www.rikvandoorn.nl/mobile/connect.php'; //connection to database
if(empty($_POST['email']) || empty($_POST['wachtwoord'])){
    $return['error'] = true;    //return error
    $return['login'] = 'Niet alle velden zijn ingevuld';
} else {
    $email = $_POST['email'];
    $wachtwoord = $_POST['wachtwoord'];

    //Check if user exists
    $sql = "SELECT * FROM gebruiker WHERE email = '$email'";
    $result = mysql_query($sql) or die(mysql_error());
    $num_rows = mysql_num_rows($result);    

    if($num_rows == 0){
        $return['error'] = true;
        $return['login'] = 'Gebruiker bestaat niet';
    } else {
        //Check if password is correct for user
        $sql_2 = "SELECT * FROM gebruiker WHERE email = '$email'  && wachtwoord = '$wachtwoord'";
        $query_2 = mysql_query($sql_2);
        $num_rows_2 = mysql_num_rows($query_2);
        if($num_rows_2 == 0){
            $return['error'] = true;
            $return['login'] = 'Wachtwoord onjuist';
        } else {
            $return['error'] = false;
            $return['login']['email'] = $email;
            $return['login']['wachtwoord'] = $wachtwoord;
        }
    }
}
echo json_encode($return);

1 个答案:

答案 0 :(得分:0)

除非你用拼写错误填充代码,否则你要混合两个完全独立的mysql库:

$con=mysqli_connect($host, $username, $password, $dbnaam);// or die($db_error1);
          ^---note the presence of an 'i'

以及其他脚本:

$result = mysql_query($sql) or die(mysql_error());
               ^---note the LACK of an 'i'

mysql和mysqli NOT 可互换,并且在一个中建立的连接对另一个完全没用。

mysql( WITHOUT 一个i)库已过时并已弃用。坚持使用mysqli( WITH 和i)。