簡體   English   中英

總是回歸假 - OOP Php

[英]Always Return False - OOP Php

我是php環境中的新手,學習如何用簡單的OOP格式進行編碼。 我即將在我的class測試我的function ,看看用戶是否已經注冊。

這是我的: 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";
}

?>

我的班級: 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;
            }

    }

}

這是我的: 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
?>

表:虛擬值 表

郵差 崗位

在我的表中存在dards@yahoo.com 所以它應該返回TRUE。

將構造函數更改為:

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

並且您的SQL語法有錯誤。 刪除. 來自$email前面。

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

我只是刪除了isset($_POST['emailAdd']) ? $_POST['emailAdd'] : ''; isset($_POST['emailAdd']) ? $_POST['emailAdd'] : ''; 來自我的test.php $email

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM