PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ); $this->PDO = new PDO($dsn,$cfg['db'][$db]['user'],$cfg['db'][$db]['pass'], $opt); } else { global $cfg; $dsn = "mysql:"; $dsn .= "host=". $cfg['db'][$dbn]['host'].";"; $dsn .= "port=". $cfg['db'][$dbn]['port'].";"; $opt = array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ); $this->PDO = new PDO($dsn,$_POST['Username'],$_POST['Password'], $opt); } } /** * execute a query that doesn't have any parameters * @param $query the mysql query * @return returns a PDOStatement object */ public function executeWithoutParams($query){ $statement = $this->PDO->prepare($query); $statement->execute(); return $statement; } /** * execute a query that has parameters * @param $query the mysql query * @param $params the parameters that are being used by the query * @return returns a PDOStatement object */ public function execute($query,$params){ $statement = $this->PDO->prepare($query); $statement->execute($params); return $statement; } /** * execute a query (an insertion query) that has parameters and return the id of it's insertion * @param $query the mysql query * @param $params the parameters that are being used by the query * @return returns the id of the last inserted element. */ public function executeReturnId($query,$params){ $statement = $this->PDO->prepare($query); $this->PDO->beginTransaction(); $statement->execute($params); $lastId =$this->PDO->lastInsertId(); $this->PDO->commit(); return $lastId; } }