0

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

MainActivity:

       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 side:

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


?>  

Sample record:

Username: admin Password: admin

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.

Dunkey
  • 1,900
  • 11
  • 42
  • 72

2 Answers2

0

http://www.php.net/manual/en/pdostatement.rowcount.php

*PDOStatement::rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed by the corresponding PDOStatement object.

If the last SQL statement executed by the associated PDOStatement was a SELECT statement, some databases may return the number of rows returned by that statement. However, this behaviour is not guaranteed for all databases and should not be relied on for portable applications.*

Check $result to see if you got a username or not.

aconrad
  • 556
  • 5
  • 12
0

(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){ 

try this under your $stmt->execute(array... line

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

or see this answer where I got this from.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141