duankaolei2921 2017-02-09 09:02
浏览 78
已采纳

致命错误:未捕获错误(php mysql)

I am learning php oop with mysql and I am having trouble fixing this code. I have checked it twice and also checked this platform to confirm.

    <?php 

require 'databasesclass.php';

$database = new Database();

$database->query('SELECT * FROM users');
$rows = $database->resultset();

print_r($rows);

and my data base class are

    <?php 


class Database{
    private $host = 'localhost:8889';
    private $user = 'root';
    private $pass = 'root';
    private $dbname = 'register';

    private $dbh;
    private $error;
    private $stmt;

    public function __construct(){
        // Set the DSN
        $dsn = 'mysql:host='. $this->host . ';dbname=' . $this->dbname . $this->user . $this->pass;
        // set options
        $options = array(

                PDO::ATTR_PERSISTENT => true,
                PDO::ATTR_ERRMODE    => PDO::ERRMODE_EXCEPTION );

        try {
            $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
        } catch (PDOException $e) {
            $this->error = $e->getMessage();
        }
    }
            public function query($query){

                $this->stmt = $this->dbh->prepare($query);

            }


            public function bind($pram, $value, $type = null){

                if (is_null($type)){
                    switch(true) {
                        case is_int($value):
                            $type = PDO::PRAM_INT;
                            break;
                        case is_bool($value):
                            $type = PDO::PRAM_BOOL;
                            break;  
                        case is_null($value):
                            $type = PDO::PRAM_NULL;
                            break;
                            default:
                            $type = PDO::PRAM_STR;  
                    }

                }

                $this->stmt->bindValue($parm, $value, $type);
            }

            public function execute(){
                 return $this->stmt->execute();
            }

            public function resultset(){
                $this->execute();
                return $this->stmt->fetchALL(PDO::FETCH_ASSOC);
            }

} 

Fatal error: Uncaught Error: Call to a member function prepare() on null in C:\MAMP\htdocs\Practice\databasesclass.php:31 Stack trace: #0 C:\MAMP\htdocs\Practice\index.php(7): Database->query('SELECT * FROM u...') #1 {main} thrown in C:\MAMP\htdocs\Practice\databasesclass.php on line 31

And the Line 31 is:

$this->stmt = $this->dbh->prepare($query);
  • 写回答

2条回答 默认 最新

  • dsi36131 2017-02-09 09:20
    关注

    No need to pass $this->user and $this->pass into your $dns variable. Remove them from there.

    So $dns variable will be

    from

    $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname . $this->user . $this->pass;
    

    to

    $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
    

    Please check and let me if you see any error there.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功