Location: PHPKode > scripts > Connection PDO Class > connection-pdo-class/connection/Connection.class.php
<?php


//Constante com caminhos para includes
define("PATH_INCLUDES", $_SERVER['DOCUMENT_ROOT']."/conn");

require_once (PATH_INCLUDES."/connection/AbsConnection.class.php");
require_once (PATH_INCLUDES."/error/Error.class.php");

/**
 * Database connection class
 * @author Adler Brediks Medrado
 * @email hide@address.com
 * @copyright 2005 - Nesher Technologies - www.neshertech.net
 * @date 30/11/2005
 */
class Connection extends AbsConnection {

	private $connection;
	private $username;
	private $password;
	private $dsn;

	/**
	 * To connect to another database, change the "mysql" in the dsn attribute to
	 * the database you want. 
	 * for example: pgsql:dbname...
	 */
	public function __construct() {
		$this->dsn = "mysql:dbname=pessoas;host=localhost";
		$this->username = "root";
		$this->password = "";
	}

	public function setConnection($conn) {
		$this->connection = $conn;
	}

	public function getConnection() {
		return $this->connection;
	}
	/**
	 * Make a connection with a database using PDO Object.
	 *
	 */
	public function connect() {
		try {
			$pdoConnect = new PDO($this->dsn, $this->username, $this->password);
			$this->connection = $pdoConnect;
		} catch (PDOException $e) {
			die("Error connecting database: Connection::connect(): ".$e->getMessage());
		}
	}
	/**
	 * Execute a DML
	 *
	 * @param String $query
	 */
	public function executeDML($query) {
		if (!$this->getConnection()->query($query)) {
			throw new Error($this->getConnection()->errorInfo());
		} else {
			return true;
		}
	}
	/**
	 * Execute a query
	 *
	 * @param String $query
	 * @return PDO ResultSet Object
	 */
	public function executeQuery($query) {
		$rs = null;
		if ($stmt = $this->getConnection()->prepare($query)) {
			if ($this->executePreparedStatement($stmt, $rs)) {
				return $rs;
			}
		} else {
			throw new Error($this->getConnection()->errorInfo());
		}
	}

	/**
	 * Execute a prepared statement 
	 * it is used in executeQuery method
	 *
	 * @param PDOStatement Object $stmt
	 * @param Array $row
	 * @return boolean
	 */
	private function executePreparedStatement($stmt, & $row = null) {
		$boReturn = false;
		if ($stmt->execute()) {
			if ($row = $stmt->fetchAll()) {
				$boReturn = true;
			} else {
				$boReturn = false;
			}
		} else {
			$boReturn = false;
		}
		return $boReturn;
	}

	/**
	 * Init a PDO Transaction
	 */
	public function beginTransaction() {
		if (!$this->getConnection()->beginTransaction()) {
			throw new Error($this->getConnection()->errorInfo());
		}
	}
	/**
	 * Commit a transaction
	 *
	 */
	public function commit() {
		if (!$this->getConnection()->commit()) {
			throw new Error($this->getConnection()->errorInfo());
		}
	}
	/**
	 * Rollback a transaction
	 *
	 */
	public function rollback() {
		if (!$this->getConnection()->rollback()) {
			throw new Error($this->getConnection()->errorInfo());
		}
	}
}
?>
Return current item: Connection PDO Class