【问题标题】:Why can't I connect to my DB为什么我无法连接到我的数据库
【发布时间】:2016-11-09 19:40:52
【问题描述】:

我已经在 AWS 上设置了一个 apache2 ubuntu 服务器,我在所述服务器中创建了一个 mySQL 数据库,现在我正在尝试从我的 android 应用程序更新数据库,但它没有连接。

这是服务器端应该连接到数据库的 PHP 文件。

<?php
 define('HOST','ec2-54-171-67-69.eu-west-1.compute.amazonaws.com');
 define('USER','root');
 define('PASS','password');
 define('DB','ugproject');

 $con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');
 ?>

这是 Java 方法。

private static final String REGISTER_URL = "ec2-54-171-67-69.eu-west-1.compute.amazonaws.com/volleyRegister.php";
 private void registerUser(){
        final String username = editTextUsername.getText().toString().trim();
        final String password = editTextPassword.getText().toString().trim();
        final String email = editTextEmail.getText().toString().trim();

        StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Toast.makeText(MainActivity.this,response,Toast.LENGTH_LONG).show();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(MainActivity.this,error.toString(),Toast.LENGTH_LONG).show();
                    }
                }){
            @Override
            protected Map<String,String> getParams(){
                Map<String,String> params = new HashMap<String, String>();
                params.put(KEY_USERNAME,username);  
                params.put(KEY_PASSWORD,password);  
                params.put(KEY_EMAIL, email);  
                return params;
            }

        };

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }

这是我得到的错误:

E/Volley: [126] BasicNetwork.performRequest: Unexpected response code 412 for http://ubuntu@ec2-54-171-67-69.eu-west-1.compute.amazonaws.com/volleyRegister.php       

我已尽量说明清楚,请随时询问更多信息,谢谢。

【问题讨论】:

  • 你检查HTTP 412 code了吗?
  • 它说它与请求标头有关,但我不确定是什么。这也是连接到数据库的 php 文件的正确格式吗?
  • 当我通过 PC 上的浏览​​器访问 http://ubuntu@ec2-54-171-67-69.eu-west-1.compute.amazonaws.com/volleyRegister.php 时,浏览器说 You are about to log in to the site “ec2-54-171-67-69.eu-west-1.compute.amazonaws.com” with the username “ubuntu”, but the website does not require authentication. This may be an attempt to trick you. - 可能是这个原因吗?错误的 URL 字符串?
  • 你是对的,这是我试图改变的错误 URL,我在正确的 URL 上得到了同样的错误:ec2-54-171-67-69.eu-west-1.compute.amazonaws.com/…

标签: php android mysql android-volley


【解决方案1】:

Error code 412 表示

前置条件失败

在服务器上测试时,一个或多个请求标头字段中给出的前提条件评估为假。此响应代码允许客户端对当前资源元信息(标头字段数据)设置先决条件,从而防止将请求的方法应用于预期之外的资源。

这可能是由于您提供的 URL 造成的

http://ec2-54-171-67-69.eu-west-1.compute.amazonaws.com/volleyRegister.php

似乎正在运行一个网络服务器,但没有正确解释 PHP 脚本 - 它向我显示 PHP 文件的原始文本。

<?php

 if($_SERVER['REQUEST_METHOD']=='POST'){
 $username = $_POST['username'];
 $email = $_POST['email'];
 $password = $_POST['password'];

 require_once('dbConnect.php');

 $sql = "INSERT INTO users (username,password,email) VALUES ('$username','$email','$password')";


 if(mysqli_query($con,$sql)){
 echo "Successfully Registered";
 }else{
 echo "Could not register";

 }
 }else{
echo 'error';
}
?>

可能你没有安装 php 插件,或者没有正确配置。

这是服务器端的问题,而不是应用端的问题。

【讨论】:

    【解决方案2】:

    试试这个可能有用。

    分离数据库名称

    $con = mysql_connect (HOST, USER, PASS) OR die ('could not connect to MySQL.');

    mysql_select_db(DB);

    【讨论】:

      【解决方案3】:

      根据您的其中一个 cmets (here) 中的链接,您似乎没有在服务器上安装 PHP,因为它正在打印代码。

      最简单的安装方法是在 Ubuntu 上使用 sudo apt-get install lamp-server^ 命令。

      如果您只需要 PHP,请使用 sudo apt-get install php7.0。安装后,使用sudo service apache2 restart 重新启动您的 apache 服务器以应用 PHP 安装。

      要测试 PHP 是否启动并运行,请将这段代码写入一个文件,当您在浏览器中访问它时,您应该会得到一个包含 PHP 安装数据的表格:

      <?php
      phpinfo();
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-03-01
        • 2015-06-20
        • 1970-01-01
        • 2012-05-13
        • 1970-01-01
        相关资源
        最近更新 更多