Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Abe Layee
Abe Layee
8,378 Points

PHP rowCount() returns 0

I'm trying to create a login system, but my rowCount() keeps returning 0. The email and password are correct correct in the database.

<?php
 ($_SERVER['REQUEST_METHOD'] == 'POST') {
  $email;
  $password;

  if (empty($_POST['email']) || empty($_POST['password'])) {
    echo "<span>All fileds are required</span>";
  } else {
    $email = validateEmail($_POST['email']);
    $password = validatePassword($_POST['password']);

    $query = "SELECT * FROM registerUser WHERE email = :email AND user_password = :user_password";
    $statement = $pdo->prepare($query);
    $statement->bindParam(':email', $email, PDO::PARAM_STR);
    $statement->bindParam(':user_password', $password, PDO::PARAM_STR);
    $row = $statement->rowCount(); 
    if($statement->execute()) {
       echo $row;
    }
     else {
       echo "Query failed";
      }
  }
}
?>
Matthew Underhill
Matthew Underhill
20,361 Points

Hi,

A few things:

Your first line is missing the if statement.

What is the code for your validateEmail and validatePassword functions?

First you need to remove these functions and just pass your SQL parameters in as plain text and see if you get a row count other than 0. If you do, then you know the problem is with your functions code. Add them back in one at a time and see where it breaks.

You can echo $email and $password to check you are getting the expected result.

You should also filter the input of the email and password POST variables immediately after your check the page request method.

You would also be better putting your database connection into a try/catch block as it will allow you to capture errors and exceptions and display them easier.

Matt