0

I have a mysql query that I am using to log into the database but for some reason it is returning -1 even though i put in the right values and the same query executes without the bind_param

$username = $_POST['username'];
$password = md5($_POST['password']);
$user_array = array('username' => $username,'password' => $password );  
$queryname = "SELECT username, password FROM user WHERE username = ? AND password = ?";

function auth($queryname,$user_array) {
        $stmt = $this->connection->prepare($queryname);
        $stmt->bind_param('ss', $user, $pass);  
        $user=$user_array['username'];
        $pass=$user_array['password'];
        $stmt->execute();       
        return $stmt->affected_rows;        
    }

Any suggestions as to how i can debug this ?

6
  • i have done that now Commented Sep 5, 2014 at 6:03
  • From the manual ~ "-1 indicates that the query has returned an error". Also, a SELECT query is never going to result in affected_rows Commented Sep 5, 2014 at 6:11
  • @Phil could there be anything wrong in my bind_param i am using an md5 hashed text, which is stored as varchar Commented Sep 5, 2014 at 6:12
  • 2
    Run this before you create your connection mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); and watch the errors come rolling in ;) Commented Sep 5, 2014 at 6:12
  • Thanks, though my solution was answered my someone else, i now know of a better way of debugging mysqli Commented Sep 5, 2014 at 6:20

1 Answer 1

1

$stmt->affected_rows returns the total number of rows changed, deleted, or inserted:

This function only works with queries which update a table. In order to get the number of rows from a SELECT query, use mysqli_stmt_num_rows() instead.

Change return $stmt->affected_rows; to:

$stmt->store_result();
$count = $stmt->num_rows;
$stmt->close();
return $count;
Sign up to request clarification or add additional context in comments.

1 Comment

@Bazinga777 also you must close $stmt: see modified code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.