简体   繁体   中英

Always Return False - OOP Php

I'm new in php environment and learning how to do coding in simple OOP format. I'm about to test my function in my class to see if the user is already registered.

here's my: testController.php

<?php


error_reporting(E_ALL^ E_DEPRECATED);

require_once('test.php');
$test = new test();

$email = isset($_POST['emailAdd']) ? $_POST['emailAdd'] : '';

if ($test->emailExist($email)) {
    echo "TRUE";
}else {
    echo "FALSE";
}

?>

My class: test.php

<?php

error_reporting(E_ALL^ E_DEPRECATED);
require_once('dbConnectClass.php');

class test{
    private $db;
    public $getEmail;

    public function __construct(){
        $this->db = new dbConnectClass();
        $this->db->connectDatabase();
    }

    public  function emailExist($email)
    {
        $getEmail = $email;
        $sql = "SELECT EmailAddress FROM useryayong WHERE `EmailAddress` = '$email' ";

        $result = mysqli_query($this->db->connect,$sql);

            $no_of_rows = mysqli_num_rows($result);
            if ($no_of_rows > 0) {
                // user exist
                return true;
            } else {
                // user not exist
                return false;
            }

    }

}

Here's my: dbConnectClass.php

<?php
//INCLUDE DB
require('dbConnect.php');

class dbConnectClass{

    //VAR Connect
    public $connect;

    //CONSTRUCTOR
    public function __construct(){}    

    //DESTRUCTOR
    public function __destruct(){}

    //METHOD - CONNECT TO DATBASE
    public function connectDatabase(){

       $this->connect = mysqli_connect(DB_SERVER,
                                       DB_USER,
                                       DB_PASSWORD,
                                       DB_DATABASE) or die(mysqli_error($this->connect));

            //SET CUSTOM ERROR MESSAGE
            if (mysqli_connect_errno()) {
                die("Database connection failed");
            }else {
                return $this->connect;    
            }

    }

    //METHOD - CLOSE DATABASE CONNECTION
    public function closeDatabaseCon(){
        mysqli_close($this->connect);
    }    

}//END CLASS
?>

TABLE : dummy values 表

POSTMAN 岗位

In my table dards@yahoo.com exist. So it should return TRUE.

Change your constructor function to this:

public function __construct(){
    $this->db = new dbConnectClass();
    $this->db->connectDatabase();
}

And there's an error on your SQL syntax. Remove the . from in front of $email .

"SELECT EmailAddress FROM useryayong WHERE `EmailAddress` = '$email'";

I just remove isset($_POST['emailAdd']) ? $_POST['emailAdd'] : ''; isset($_POST['emailAdd']) ? $_POST['emailAdd'] : ''; from $email in my test.php .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM