安卓登录mysql数据库 [英] Android login to mysql database

查看:46
本文介绍了安卓登录mysql数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在 android 中做一个简单的登录系统到 mysql 数据库在线.这是我迄今为止尝试过的:

Hello guys I'm doing an simple login system in android to mysql database online. This is what I've tried so far:

主要活动:

       protected String doInBackground(String... args) {

        strUsername = etUsername.getText().toString();
        strPassword = etPassword.getText().toString();

        try{
            String data  = URLEncoder.encode("username", "UTF-8") 
            + " = " + URLEncoder.encode(strUsername, "UTF-8");
            data += "&" + URLEncoder.encode("password", "UTF-8") 
            + " = " + URLEncoder.encode(strPassword, "UTF-8");

            URL url = new URL(url_login);
            URLConnection conn = url.openConnection(); 
            conn.setDoOutput(true); 

            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); 
            wr.write( data ); 
            wr.flush(); 

            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            StringBuilder sb = new StringBuilder();
            String line = null;
            // Read Server Response
            while((line = reader.readLine()) != null)
            {
               sb.append(line);
               break;
            }

            Log.e("TAG", sb.toString());

            return sb.toString();


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


    }

PHP 端:

<?php
// Connect to dbconnect.php
include('dbconnect.php');

// array for JSON response 
$response = array();

// username and password sent from form
$username = $_POST['username'];
$password = $_POST['password'];



$stmt = $conn->prepare('SELECT username, password FROM customer WHERE username = :username AND  password = :password');
$stmt->execute(array(':username' => $username, ':password' => $password ));


$result = $stmt->fetch(PDO::FETCH_ASSOC);
$user = $result['username'];
$affected_rows = $stmt->rowCount();

    if($affected_rows >= 1){
         $response["success"] = 1;
         // successfully inserted into database

     $response["message"] = "New record successfully created.";
         // echoing JSON response
     echo json_encode($response);
    }
    else{
        $response["success"] = 0;
        echo json_encode($response);
    } 


?>  

样本记录:

用户名:admin密码:admin

Username: admin Password: admin

我试图运行它,但它得到了一个响应:success:0,这意味着不成功.我在这里做错了什么?我很乐意感谢您的帮助.非常感谢.

I tried to run this but it's getting a response: success: 0 which means not successful. What am I doing wrong in here? I would gladly appreciate your help. Thanks a lot.

推荐答案

(根据要求评论回答)

而不是所有这些:

(Comment to answer, as requested)

Instead of all of this:

$result = $stmt->fetch(PDO::FETCH_ASSOC); 
$user = $result['username']; 
$affected_rows = $stmt->rowCount(); 
if($affected_rows >= 1){ 

在你的 $stmt->execute(array...

if($stmt->rowCount() > 0) 
  { echo "Exists"; }
else { echo "Does not exist"; }

或查看这个答案我从哪里得到的.

or see this answer where I got this from.

这篇关于安卓登录mysql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆