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 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效