dongzhuonao8429 2016-03-26 08:48
浏览 58

在非对象上调用成员函数prepare()(致命错误)

Fatal error: Call to a member function prepare() on a non-object in G:\xampp\htdocs\live\Billing Suryas\model\DBConfig.php on line 28

<?php
    class Database
    {   
        private $host = "localhost";
        private $db_name = "new_suryas1";
        private $username = "root";
        private $password = "";
        public $conn;
        public function dbConnection()
        {
            $this->conn = NULL;    
            try
            {
                $conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
                $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
            }
            catch(PDOException $exception)
            {
                echo "Connection error: " . $exception->getMessage();
            }

            return $conn;
        }
        public function login($usname,$uspswd)
        {
            try
            {
                $stmt =$conn->prepare("select * from users where user_name=:uname and password=:paswrd and permission='0' and delet='0'");
                $stmt->execute(array(':uname'=>$usname, ':paswrd'=>$uspswd));
                $userRow=$stmt->fetch(PDO::FETCH_ASSOC);
                if($stmt->rowCount() == 1)
                {
                    if(password_verify($uspswd, $userRow['password']))
                    {
                        $_SESSION['user_session'] = $userRow['user_id'];
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch(PDOException $exception)
            {
                echo $exception->getMessage();
            }
        }

    }
    ?>

This is my DBConfig.php

I can't find what is the error in my code

anyone help me please...

  • 写回答

2条回答 默认 最新

  • dongluanjie8678 2016-03-26 08:55
    关注

    You write a class with these properties:

    class Database
    {   
        private $host = "localhost";
        private $db_name = "new_suryas1";
        private $username = "root";
        private $password = "";
        public $conn;
    

    Inside a class method, variable scope is same as functions: external variables are not accessible.

    To access to class properties inside a class method you have to use $this:

    $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
    $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    
    (...)
    
    $stmt = $this->conn->prepare("select * from users where user_name=:uname and password=:paswrd and permission='0' and delet='0'");
    

    评论

报告相同问题?

悬赏问题

  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂