duanpu1064 2015-06-08 15:17
浏览 154
已采纳

mysqli_query()期望参数1是mysqli,我不能使用变量?

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
{
    protected $connection;
    function __construct()
    {
        $this->connection = 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($this->connection, "SELECT * FROM users WHERE LOWER (Username) = LOWER ('$username') AND Password = '$password'");
        $dataResult = mysqli_fetch_array($result);
        $row = mysqli_num_rows($result);
        if($row == 1)
        {
            $data = array();
            $data['loggedIn'] = true;
            $data['Username'] = $dataResult["Username"];
            $data['id'] = $dataResult["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;
        }
    }
}

When i use userLogin function (on index.php) i recive this errors:

Warning: mysqli_query() expects parameter 1 to be mysqli, object given in ...\class.User.php on line 35

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in ...\class.User.php on line 36

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in ...\class.User.php on line 37 Failed

How can i solve this?

  • 写回答

1条回答 默认 最新

  • dongshun5963 2015-06-08 15:35
    关注

    In db.php

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

    In class.User.php:

    protected $dbConnect;
    protected $connection;
    
    function __construct()
    {
        $this->dbConnect = new dbConnect();
        $this->connection = $this->dbConnect->Connect();
    }
    
    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($this->connection, $insert);
        return $data;
        }
    }
    

    By the way, you should get out of the habit of not declaring the scope on your class methods, "public function" rather than "function".

    You would also be better to learn Dependency Injection rather than creating the the dependencies you are creating here. You would instead have something like:

    //Sourced from a config file
    $config_array = array("username" => ***, "password" => ****, ...);
    
    $database = new dbConnect($config_array);
    $userFunctions = new userFunction($database);
    

    Disclaimer -- this is only an extremely basic example of dependency injection. I would highly recommend you read a book that covers single responsibility, inheritance, dependency injection, etc. The goal is to create reusable classes with well-defined interfaces allowing you the flexibility to manipulate how a task is completed without needing to find and re-write numerous other files. When it comes to the benchmark question in this example, "how easy would it be to switch to a different database", if your answer is anything other than "pretty easy", you've got too many dependencies. Granted, most people don't switch databases, but take a little extra time to grow as a programmer -- you will thank yourself later when your application has more than a handful of files and you need to debug. Unit testing gets thrown around a lot, but the real answer is, the concepts of unit testing can help you write more maintainable code even if you aren't actually unit testing everything.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料