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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!