0
<?php
class DbOperations
{
    private $con;

    function __construct()
    {
        require_once dirname(__FILE__).'/DbConnect.php';

        $db = new DbConnect();

        $this->con = $db->connect();
    }

    public function createUser($email, $password, $gender, $dob_year, $dob_month, $dob_day, $time_registered)
    {
        if($this->isUserExist($email))
        {
            echo "0";
            return 0;
        }
        else
        {
            $stmt = $this->con->prepare("insert into table_user (`id`, `email`, `password`, `gender`, `dob_year`, `dob_month`, `dob_day`, `time_registered`) values (NULL, ?, ?, ?, ?, ?, ?, ?);");

            $stmt->bind_param("sssssss", $email, $password, $gender, $dob_year, $dob_month, $dob_day, $time_registered);

            if($stmt->execute())
            {
                echo "1";
                return 1;
            }
            else
            {
                echo "2";
                return 2;
            }
        }
    }

    private function isUserExist($email)
    {
        $stmt = $this->con->prepare("select id from table_user where email = ?");
        $stmt->bind_param("s", $email);
        $stmt->execute();
        $stmt->store_result();

        return $stmt->num_rows > 0;
    }
}
?>

Hi, I am trying to make a registration page. createUser function works perfectly when I return true or false from isUserExists() method. It seems like $stmt->num_rows > 0; always returning false, but I see that data is saved into the database. It is supposed to change return value to true when user registers, but, it always return false. What is wrong with this?

Eric
  • 159
  • 14
  • What's the return value of `$this->con->prepare`, `$stmt->execute();` and `$stmt->store_result()` ? Also, do you have error reporting enabled? (See [How do I get PHP errors to display](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display)) – ccKep Oct 19 '17 at 21:35
  • both are 1 and 1 – Eric Oct 19 '17 at 21:37

1 Answers1

0

You have a syntax problem and the parameters too. Is "bindParam", not "bind_param". This same problem occurs in insert too. See the examples in the documentation: http://php.net/manual/en/pdostatement.bindparam.php.

Use:

$stmt = $this->con->prepare("select id from table_user where email = ?");
$stmt->bindParam("1", $email, PDO::PARAM_STR);

Or:

$stmt = $this->con->prepare("select id from table_user where email = :email");
$stmt->bindParam(":email", $email, PDO::PARAM_STR);
  • This is likely mysqli, not PDO. It's [bind_param](http://php.net/manual/de/mysqli-stmt.bind-param.php). – ccKep Oct 20 '17 at 09:18