dongyi8416 2018-07-12 09:25
浏览 61

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

I'm trying to perform CRUD operation by using instance of database class I've created here(class db), but I'm not getting database connection instance into another class, I'm using Netbeans IDE and MySql Database

<?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;
    }
}
?>

Given below is CRUD class

<?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;
        }
    }
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大