【问题标题】:Cannot connect to mysql server with my phonegap app无法使用我的 phonegap 应用程序连接到 mysql 服务器
【发布时间】:2013-10-03 22:16:45
【问题描述】:

我最近刚刚使用 jquery 完成了我的移动应用程序,它可以在我的网络浏览器上完美运行,但是当我使用 phone gap 编译它时。我什至无法登录,当我通过 ajax 调用它们时,似乎我的 php 文件不起作用,并且正在使用外部免费 mysql 托管站点,并且我的所有数据都已设置并准备好这是我的代码

登录页面的html端

<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处理数据并提交到php文件

$('#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;
       });

我的php端代码

<?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';
}
?>

【问题讨论】:

    标签: javascript php jquery mysql cordova


    【解决方案1】:

    哇...您真的想分享您的完整数据库凭据吗?

    您应该在 ajax 中发布您遇到的错误 - 例如:textStatus 或 xhr.status/xhr.state 的值

    另外,您可以使用 GET 方法(仅用于测试),然后在查询字符串中提交用户的登录字段和值。从那里,您可以确认 php 正在提供结果并且没有引发错误。即你可以直接从移动设备的浏览器而不是通过 ajax 来完成(如果你还没有测试过的话)

    其他需要查看的内容 - 检查 phonegap 的 config.xml 文件(靠近底部)。您需要将域设置为有效 - 一旦所有内容都内置到 PG 中,这可能是一个大域名...

    最后,查看“SQL 注入”并确保在查询字符串中使用提交的用户名/密码之前应用 real_escape_string...

    【讨论】:

    • 我查看了在 ajax 成功回调函数中返回了哪些数据,我发现它正在返回我的 html 语法。好困惑。帮助!
    猜你喜欢
    • 1970-01-01
    • 2012-09-17
    • 1970-01-01
    • 1970-01-01
    • 2021-04-22
    • 1970-01-01
    • 2015-09-13
    • 2021-06-14
    • 2021-04-12
    相关资源
    最近更新 更多