How to close a connection to the database using PDO?

3

I'm new to the PHP development area and my app uses a method to open a connection to the database in MySQL , I'm looking to update and display the records.

I need to use some method to close the connection, type: function close() ?

public function ($)
    {
        $sql  = "";
        try
        {
            $stmt = connectionDB::prepare($sql);
            if($stmt->execute())
            {
            }
        } catch (PDOException $e){
            echo "Err ->" . $e->getMessage();
        }

        finally{
             try {
                 $stmt = connectionDB::close();
             }
        }
}

My class:

class ClassDao extends Dao {

    public function insert($registro)
    {
        $sql  = "INSERT INTO table () VALUES ()";
        try
        {
            $stmt = connectionDB::prepare($sql);
            if($stmt->execute())
            {
                echo "INSERT!!";
            }
        } catch (PDOException $e){
            echo "Err ->" . $e->getMessage();
        }

    }

    public function update($registro)
    {
        $sql  = "UPDATE table SET var = :var, WHERE var = :var";
        try
        {
            $stmt = connectionDB::prepare($sql);
            if($stmt->execute()){
                echo "UPDATE!!";
            }
        } catch (PDOException $e){
            echo "Err ->" . $e->getMessage();
        }
    }

    public function findAll()
    {
        $sql = "SELECT * FROM table";
        try
        {
            $stmt = connectionDB::prepare($sql);
            if($stmt->execute()){
                return $stmt->fetchAll(PDO::FETCH_CLASS,'table');
            }
            else {
                return new Class();
            }
        } catch (PDOException $e){
            echo "Err ->" . $e->getMessage();
        }
    }

    public function createObject($r)
    {
        try{
            $var= new Class($r['var1'], $r['var2']);
            return $var;
        } catch (PDOException $e){
            echo "Err ->" . $e->getMessage();   
        }
    }
}
    
asked by anonymous 31.08.2015 / 15:40

1 answer

6
$pdo = null;

You can use as in the example above but is not required because the connection is closed automatically after the script is executed.

    
31.08.2015 / 19:04