Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP数据库连接错误,无法获取数据库类实例_Php_Mysql_Database_Object_Pdo - Fatal编程技术网

PHP数据库连接错误,无法获取数据库类实例

PHP数据库连接错误,无法获取数据库类实例,php,mysql,database,object,pdo,Php,Mysql,Database,Object,Pdo,我试图通过使用我在这里创建的数据库类的实例(类db)来执行CRUD操作,但我并没有将数据库连接实例放入另一个类中,我使用的是Netbeans IDE和MySql数据库 <?php class db { private $dsn="mysql:host=localhost;dbname=db_stud"; private $username="root"; private $password=''; protected $connection;

我试图通过使用我在这里创建的数据库类的实例(类db)来执行CRUD操作,但我并没有将数据库连接实例放入另一个类中,我使用的是Netbeans IDE和MySql数据库

<?php
class db
{
    private $dsn="mysql:host=localhost;dbname=db_stud";
    private $username="root";
    private $password='';  
    protected $connection;

    public function openConnection() 
    {
        try
        {
            $this->connection=new PDO($this->dsn, $this->username, $this->password);
            // set the PDO error mode to exception
            $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);   

            return $this->connection;
        } catch (PDOException $ex) {
            print("Error ".$ex->getMessage()."<br/>");
            die();
        }
    }

    public function closeConnection()
    {
        $this->connection=null;
    }
}
?>

下面给出的是积垢等级

<?php
require_once("./db6.php");
class crud
{
    public $id;#function to insert record into database
    protected $dbcon;

    public function insert($studentArr)
    {
        $sql="INSERT INTO 'student' ('fname','lname','gender','batch','address','mobile'"
    . ",'age','dob','profilepic','fees') VALUES(:fname,:lname,:gender,:batch,:address"
    . ":mobile,:age,:dob,:profile,:fees)";
        $this->dbcon=new db;
        $db= $this->dbcon->openConnection();

        $result= $db->prepare($sql);
        $pdoresult=$result->execute(array(":fname"=>$fname,":lname"=>$lname,":gender"=>$gender,":batch"=>$std,":address"=>$address,
    ":mobile"=>$mobile,":age"=>$age,":dob"=>$dob,":profile"=>$profile,":fees"=>$fees));
        if($pdoresult)
        {
            $lastid=$db->lastInsertId();
            $this->id=$lastid;
        }
    }
}

为什么不这样做,而是将db类的实例传递给crud类的构造函数?对于数据库的go singleton设计模式,否则每次都只需创建新的连接,否则至少在使用完后关闭连接。它表示您遇到了错误:错误是什么?