如何使用类中的变量?

时间:2015-06-08 10:38:09

标签: php sql class

我不知道如何使用dbConnect中的$ connection变量。

我需要将此变量用于userLogin函数:mysqli_query($ connection,...)但是没有声明..如果我使用没有$ connection的mysqli_query,如mysqli_query(" SELECT * .. ..")我发现了这个错误:

  

警告:mysqli_query()需要至少2个参数,1在

中给出      

和其他2个错误:警告:mysqli_fetch_array()需要参数1   是mysqli_result,在第35行的...... \ class.User.php中给出了null

     

警告:mysqli_num_rows()期望参数1为mysqli_result,   在第36行的...... \ class.User.php中给出的null

我该如何解决这个问题?

的index.php

<?php
if(!defined('ROOT'))
{
    define('ROOT','C:/Apache/htdocs/website/inc');
}
require_once ROOT. '/config/db.php';
require_once ROOT. '/library/classes/class.User.php';
$checkUser = new userFunction(); 
if($_SERVER["REQUEST_METHOD"] == "POST")
{
    $username = $_POST["username"];
    $password = $_POST["password"];
    $user = $checkUser->userLogin($username, $password);
    if($user)
    {
        echo "Succes";
    }
    else
    {
        echo "Failed";
    }
}

?>

db.php中

<?php

    class dbConnect
    {
        function __construct()
        {
            require_once('config.php');
            $connection = mysqli_connect(hostname, username, password);
            mysqli_select_db($connection, database);
            if (!$connection)
            {
                die('Could not connect to the database because: ' .mysqli_connect_error());
            }
            return $connection;
        }
        public function Close()
        {
            mysqli_close();
        }
    }

?>

class.User.php

<?php
if(!defined('ROOT'))
{
    define('ROOT','C:/Apache/htdocs/website/inc');
}
require_once ROOT. '/config/db.php';
session_start();

class userFunction
{
    function __construct()
    {
        $database = new dbConnect();
    }

    function __destruct()
    {

    }

    public function userRegister($username, $password, $checkpassword)
    {
        $password = md($password);
        if ($password == $checkpassword)
        {
        $insert = "INSERT INTO users (Username, Password) VALUES ('$username','$password')";
        $data = mysqli_query($connection, $insert);
        return $data;
        }
    }

    public function userLogin($username, $password)
    {
        $result = mysqli_query("SELECT * FROM users WHERE LOWER (Username) = LOWER ('$username') AND Password = '$password'");
        $data = mysqli_fetch_array($result);
        $row = mysqli_num_rows($result);
        if($row == 1)
        {
            $data = array();
            $data['loggedIn'] = true;
            $data['Username'] = $data["Username"];
            $data['id'] = $data["userID"];
            $data['loginDate'] = time();
            $data['lastAccess'] = time();
            $data['rememberMe'] = $rememberMe;
            $data['ip'] = $ip;
            $data['via'] = $via;
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }

    public function isUserExist($username)
    {
        $result = mysqli_query($connection, "SELECT * FROM users WHERE LOWER (Username) = LOWER ('$username')");
        $row = mysqli_num_rows($result);
        if ($row > 0)
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

dbConnect class'构造函数创建了您需要在userFunction中使用的连接。 userFunction的构造函数创建了dbConnect的新实例。

所以这里:

class dbConnect {
    function __construct() {
        // ...
        return $connection; // That's the $connection you want to use in another class. Constructors can't return any value.
    }
}

为了能够从另一个范围访问该变量,您需要将其作为类的property,并将其公开(我更喜欢在此处创建一个公共方法):

class dbConnect {
    protected $connection; // The property where your connection will be stored.

    function __construct() {
        // ...
        $this->connection = $connection; // That's the $connection you want to use in another class.
    }

    public function getConnection() {
    // A function to access this property from another class.
         return $this->connection;
    }
}

现在在userFunction

class userFunction {

    function __construct() {
        $database = new dbConnect();
        // That $database variable has a $connection property and we want to use it in other methods of this class.
    }
...

我们正在创建一个新属性,以便能够在班级的任何位置使用$database

class userFunction {

    protected $database;

    function __construct() {
        $this->database = new dbConnect();
    }

    function userLogin() {
        // Access the variable you need by calling $this->database->getConnection() here.
    }
...