简体   繁体   中英

Cannot connect to mysql server with my phonegap app

I just recently completed my mobile app using jquery and it runs perfectly on my web browser but when i compile it using phone gap. I can't even log in and it seems like my php files don't work when i call them via ajax, and am using an external free mysql hosting site and all my data is set and ready this is my code

html side for the login page

<a href="#" data-transition="slideup" id="login" data-add-back-btn="true" data-iconpos="right" style="float:right;" data-icon="arrow-r">Login</a>

 <form ajax="true" class="login-user" action="php/li.php" method="POST">
<table style=" text-decoration:none;">
<tr>
        <td>
        <label>Username</label>
        </td>
        <td colspan="1">
        <input name="username" id="Lusername" placeholder="username" type="text" >
        </td>
    </div>
    </tr>
    <tr>
    <td>
    <div align="center" data-role="fieldcontain" style="width:100%;overflow:hidden" data-appbuilder-object="input" data-position="static">
        <label>Password</label>
        </td>
        <td colspan="1">
        <input name="password" id="Lpassword" placeholder="password" type="password" >
        </td>
        </tr>
        </table>
    </div>
    </form>

javascript to handle data and submit to php file

$('#login').live('click', function(){
       var that = $('form.login-user'),
       urls = that.attr('action'),
       methods = that.attr('method'),
       data = {};
      data = that.serialize();
        console.log(data);


       $.ajax(
    {
        url: urls,
        type: methods,
        //dataType: 'json',
        data : data,
        beforeSend: function(response){},
        success: function(ret){
            if(ret == true)
            {
               $.mobile.changePage('gbalet.html');
               refreshPage();
            }else if(ret == false)
            {
                alert('Incorrect Username or Password');
            }
            else
            {
                alert('Cannot Connect to Server');
            }
        },
        error: function(xhr, textStatus, errorThrown){alert("Check Internet Connection\nCannot Connect to Server");
        },
        complete: function(response){},
    }
    );
       return false;
       });

My php side code

<?php
 //connecting to remotedatabase
 $con =  mysql_connect("sql4.freemysqlhosting.net", "sql419167","dJ8%vC7%") or  die("could not connect to server ".mysql_error());
   $db = mysql_select_db("sql419167")or die("could not connect to database".mysql_error());

function signin($nameoremail, $password)
{
    $query = "SELECT `username`, `password` FROM `userinfo` WHERE `username` = '$nameoremail' OR `email` = '$nameoremail' AND `password` = '$password'";
    $quring = mysql_query($query) or die(mysql_error());
    if($quring)
    {
        if(mysql_num_rows($quring) < 1)
        {
                return false;
        }
        else
        {
            $data = mysql_fetch_assoc($quring);
            $_SESSION['username'] = $data['username'];
            return true;
        }

    }
    else
    {
        return false;
    }
}

$name =  htmlentities($_POST['username']);//htmlentities($values[0]);
$password =  htmlentities($_POST['password']);//htmlentities($values[2]);

if(isset($name) && isset($password))
{
$value = signin($name,$password);
echo $value;
session_start();
$_SESSION['username'] = $name;
}
else
{
echo 'not set';
}
?>

Wow... you really want to share your full DB credentials?

You should post what error you're getting in ajax - ex: the value of textStatus or xhr.status/xhr.state

Also, you could use the GET method (just for testing), then submit the user's login fields and values in the query string. From there, you can confirm that php is providing the results and not throwing an error. ie you can do it straight from the mobile device's browser instead of via ajax (if you haven't already tested that)

Other things to look at - check phonegap's config.xml file (near the bottom). You need to set the domain as valid - this might be the big one once everything is built into PG...

Finally, look into 'SQL Injection' and be sure to apply real_escape_string before using the submitted username/password in your query string...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM