【问题标题】:Getting "<HTML>" instead of query result using php to access database from android使用php获取“<HTML>”而不是查询结果从android访问数据库
【发布时间】:2016-07-22 10:25:18
【问题描述】:

我正在使用this tutorial 来实现一个简单的用户登录活动。我是 php 新手,我只是不知道我做错了什么。在他为他的查询获取“角色”属性的教程中,我得到一个字符串 我想看看我的代码是否得到任何结果。 这是我的 LoginActivity。

public class LoginActivity extends AppCompatActivity {



   BufferedReader in;
    private int byGetOrPost = 0;
    int success = 0;
    String id1 = "";
    String password1 = "";
    private static final String TAG_SUCCESS = "success";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

//        Defining onClickListener for Login Button
        Button loginBtn = (Button) findViewById(R.id.login_btn);
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Check credentials
                EditText phone = (EditText) findViewById(R.id.phone_txt);
                EditText pwd = (EditText) findViewById(R.id.password_txt);
                id1 = phone.getText().toString();
                password1 = pwd.getText().toString();
                new SigninActivity().execute(id1, password1);
            }
        });
    }

    public class SigninActivity extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... arg0) {
            if(byGetOrPost == 0){ //means by Get Method

                try{
                    String id = (String)arg0[0];
                    String password = (String)arg0[1];
                    String link = "#######/android_connect/check_user_login.php?id="+id+"&password="+password;

                    URL url = new URL(link);
                    HttpClient client = new DefaultHttpClient();
                    HttpGet request = new HttpGet();
                    request.setURI(new URI(link));
                    HttpResponse response = client.execute(request);
                    BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

                    StringBuffer sb = new StringBuffer("");
                    String line="";

                    while ((line = in.readLine()) != null) {
                        sb.append(line);
                        break;
                    }
                    in.close();
                    return sb.toString();
                }

                catch(Exception e){
                    return new String("Exception: " + e.getMessage());
                }
            }

        }

        @Override
        protected void onPostExecute(String result){
            TextView reg=(TextView) findViewById(R.id.register_txt);
            reg.setText(result);
        }
    }
}

这是我的 PHP 文件 check_user_login.php

<?php
$con=mysqli_connect("###.###.###.##:####","####","######","heycab");

if (mysqli_connect_errno($con))
{
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$id= $_POST['id'];
$password = $_POST['password'];
$result = mysqli_query($con,"SELECT Role FROM tb_login where 
id=$id and Password='$password'");
$row = mysqli_fetch_array($result);
$data = $row[0];

if($data){
echo $data;
}
mysqli_close($con);
?>

但最后,无论我输入什么凭据,“reg”文本视图始终显示此文本。 ""

【问题讨论】:

  • 您是否在活动中遇到异常?并且您尝试记录结果?
  • 如果我的代码返回异常,它应该在我的“reg”文本视图中显示“异常:...”。如果这就是你的意思,我的应用程序不会崩溃或强制停止。它只显示带有尖括号的字符串“HTML”。
  • 把 android 排除在外。使用浏览器调试您的 php 脚本。查看网络服务器日志或启用错误以查看您的 php 脚本中出了什么问题
  • 再次,我对 php 还是很陌生,实际上我不知道如何在不从 java 代码中调用它的情况下运行它。我喜欢直接在浏览器中打开php文件吗?

标签: php android mysql database


【解决方案1】:

所以当我在浏览器上打开 php 时看不到错误,因为放置文件的服务器将其 error_display 设置设置为 false。所以我改用Xamp。将文件放在目录“xamp/htdocs/”中,然后从 Xamp 控制面板打开 Apache 和 MySQL 后,我在浏览器中输入文件 url,“localhost/htdocs/file_name.php”。现在它向我展示了我以前看不到的错误。因此,如果其他人遇到同样的问题,他们可以使用此方法来识别其 php 文件中的错误。哦,我的数据库的用户名和密码错误。万一有人想知道。

【讨论】:

    猜你喜欢
    • 2011-03-06
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    • 2015-10-10
    • 2014-03-23
    • 2021-07-31
    • 2017-04-25
    相关资源
    最近更新 更多