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条)

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染