无法从我的数据库类访问连接详细信息

hi i am using oophp to create a registration and login system, i am trying to check if an email or username already exists in the database, but when i try and call the database class i keep getting these errors, any help would be appreciated.

Notice: Undefined variable: Database in C:\Users\josh\xampp-win32-1.8.3-5-VC11\xampp\htdocs\oop_system\include\class.user.php on line 14

Notice: Trying to get property of non-object in C:\Users\josh\xampp-win32-1.8.3-5-VC11\xampp\htdocs\oop_system\include\class.user.php on line 14

Fatal error: Call to a member function query() on a non-object in C:\Users\josh\xampp-win32-1.8.3-5-VC11\xampp\htdocs\oop_system\include\class.user.php on line 14

user class

<?php 
    include "db_config.php";

    class User{

        /*** for registration process ***/
        public function reg_user($name,$username,$password,$email){


            $password = md5($password);
            $sql="SELECT * FROM users WHERE uname='$username' OR uemail='$email'";

            //checking if the username or email is available in db
            $check =  $Database->getConnection->query($sql) ;
            $count_row = $check->num_rows;

            //if the username is not in db then insert to the table
            if ($count_row == 0){
                $sql1="INSERT INTO users SET uname='$username', upass='$password', fullname='$name', uemail='$email'";
                $result = mysqli_query($this->db,$sql1) or die(mysqli_connect_errno()."Data cannot inserted");
                return $result;
            }
            else { return false;}
        }
    }
?>

db_config class

<?php
/*
* Mysql database class - only one connection alowed
*/
class Database {
    private $_connection;
    private static $_instance; //The single instance
    private $_host = "localhost";
    private $_username = "root";
    private $_password = "";
    private $_database = "oop";
    /*
    Get an instance of the Database
    @return Instance
    */
    public static function getInstance() {
        if(!self::$_instance) { // If no instance then make one
            self::$_instance = new self();
        }
        return self::$_instance;
    }
    // Constructor
    private function __construct() {
        $this->_connection = new mysqli($this->_host, $this->_username, 
        $this->_password, $this->_database);

        // Error handling
        if(mysqli_connect_error()) {
            trigger_error("Failed to conencto to MySQL: " . mysql_connect_error(),
                 E_USER_ERROR);
        }
    }
    // Magic method clone is empty to prevent duplication of connection
    private function __clone() { }
    // Get mysqli connection
    public function getConnection() {
        return $this->_connection;
    }
}
?>
donglian6625
donglian6625 我的不好,谢谢。
大约 5 年之前 回复
doulai8405
doulai8405 用getConnection()替换getConnection。
大约 5 年之前 回复
drzfz9995
drzfz9995 $check=Database::getInstance()->getConnection()->query($sql);
大约 5 年之前 回复

1个回答




  1. 数据库</ code>是一个类,而不是变量,所以你用数据库</ code>,而不是 $ Database </ code>。</ li>
  2. getConnection </ code>是一个函数/方法而不是变量/字段,所以你' ll必须在其名称后使用括号: getConnection()</ code>。</ li>
  3. getConnection </ code>是一个非静态函数,所以你不能调用 在 Database </ code>上,您必须获取该类的实例(使用 Database :: getInstance()</ code>)。</ li>
    </ ol> \ n

    总而言之,替换这一行:</ p>

      $ check = $ Database-&gt; getConnection-&gt; query($ sql); 
    </ 代码> </ pre>

    用这个:</ p>

      $ check = Database :: getInstance() - &gt; getConnection() - &gt; query(  $ sql); 
    </ code> </ pre>
    </ div>

展开原文

原文

  1. Database is a class, not a variable, so you call it with Database, not $Database.
  2. getConnection is a function/method and not a variable/field, so you'll have to use parentheses after its name: getConnection().
  3. getConnection is a non-static function, so you can't call it on Database, you'll have to get an instance of that class (using Database::getInstance()).

All in all, replace this line:

$check = $Database->getConnection->query($sql);

with this:

$check = Database::getInstance()->getConnection()->query($sql);

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问