php数据库连接错误,无法获取数据库类示例

zbdgwd5y  于 2021-06-21  发布在  Mysql
关注(0)|答案(0)|浏览(273)

我试图通过使用我在这里创建的数据库类的示例(类db)来执行crud操作,但我并没有将数据库连接示例引入另一个类,而是使用netbeanside和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;
        }
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题