致命错误:在第15行的非对象上调用成员函数prepare()

时间:2015-04-12 05:41:01

标签: php oop pdo mysqli phpmyadmin

任何人都可以帮助我找出这个代码博客中的错误吗?代码在这里:..........

<?php
include_once('classdatabase.php');

class ManageUsers{
    public $link;

    function __construct(){
        $db_connection= new dbConnection();
        $this->link=$db_connection->connect();
        return $this->link;
    }

    function registerUsers($username,$password){
       // include_once('classdatabase.php');
        $query=$this->link->prepare("insert into users (username, password) VALUES (?,?)");// the error is just noticed this line !
        $values=array($username,$password);
        $query->execute($values);
        $count=$query->rowCount();
        return $count;
    }
}
$users= new ManageUsers();
echo $users->registerUsers('emi','emi');


?>

接下来这是另一个PHP类文件(classdatabase.php),其中我被声明为主数据库连接,代码在这里:

<?php
class dbConnection{
   protected $db_conn;
    public $db_name='todo';
    public $db_user='root';
    public $db_pass='';
    public $db_host='localhost';

    function connect(){
    try{   $this->db_conn= new PDO("mysql:host= $this->db_host;dbname=$this->db_name",$this->db_user,$this->db_pass);
        return $this->db_conn;
    } catch(PDOException $e){
        return $e->getMessage();
    }
    }
}

?>

有错误,错误 -

  

致命错误:在第15行的C:\ wamp \ www \ Todo \ Classes \ class.ManageUsers.php中的非对象上调用成员函数prepare()

1 个答案:

答案 0 :(得分:1)

您需要访问$this->db_conn。试试这些mods。我已经在我自己的数据库上进行了测试,它可以正常工作。如果您仍然遇到问题,那么您可能需要检查您是否使用了正确的连接凭据,因为这不会起作用的唯一方法是连接失败并返回错误而不是类($this ):

class dbConnection
    {
        // Create public connection
        public $db_conn;
        public $db_name =   'todo';
        public $db_use  =   'root';
        public $db_pass =   '';
        public $db_hos  =   'localhost';

        public  function connect()
            {
                try {
                        // Try concatenating your db credentials
                        $this->db_conn = new PDO('mysql:host='.$this->db_host.';dbname='.$this->db_conn,$this->db_user,$this->db_pass);
                        // return $this only
                        return $this;
                    }
                catch(PDOException $e) {
                        // Try exiting and printing the error.
                        print_r($e->errorInfo());
                        exit;
                    }
            }
    }

    class ManageUsers
        {
            public  $link;
            public  function __construct(){
                $db_connection  =   new dbConnection();
                // Assign method chained connection
                $this->link     =   $db_connection->connect()->db_conn;
            }

            function registerUsers($username,$password){
               // include_once('classdatabase.php');
                $query  =   $this->link->prepare("insert into users (username, password) VALUES (?,?)");// the error is just noticed this line !
                $values =   array($username,$password);
                $query->execute($values);
                $count  =   $query->rowCount();
                return $count;
            }
        }

$users  =   new ManageUsers();
echo $users->registerUsers('emi','emi');