Jump to content

really weird problem with wrapped PDO


next

Recommended Posts

I wrapped PDO in my class but have a weird problem with my execute method:

<?php 		
class Database {
	private static $dbh;

	private function __construct() {}

	private static function connect() {
		if(!isset(self::$dbh)) {
			try {
				self::$dbh = new PDO(DB_DSN, DB_USER, DB_PASSWORD, array(ATTR_PERSISTENT => true));
			}
			catch(PDOException $e) {
				self::close();
				trigger_error($e->getMessage(), E_USER_ERROR);
			}
		}

	return self::$dbh;
	} 

	public static function close() {
		self::$dbh = null;
	}

	public static function execute($sql) {
		try {
			$handle = self::connect();
			$stmt = $handle->prepare($sql);
			$stmt->execute();
		}
		catch(PDOException $e) {
			self::close();
			trigger_error($e->getMessage(), E_USER_ERROR);
		}
	}

	public static function getAll($sql, $style = PDO::FETCH_ASSOC) {
		$result = null;

		try {
			$handle = self::connect();
			$stmt = $handle->prepare($sql);
			$stmt->execute();
			$result = $stmt->fetchAll($style);
		}
		catch(PDOException $e) {
			self::close();
			trigger_error($e->getMessage(), E_USER_ERROR);
		}

	return $result;
	}

	public static function getRow($sql, $style = PDO::FETCH_ASSOC) {
		$result = null;

		try {
			$handler = self::connect();
			$stmt = $handler->prepare($sql);
			$stmt->execute();
			$result = $stmt->fetch($style);
		}
		catch(PDOException $e) {
			self::close();
			trigger_error($e->getMessage(), E_USER_ERROR);
		}

	return $result;
	}
}
?>

If you will look at getAll or getRow you'll notice that i have to type in the same lines of code:

			$handler = self::connect();
			$stmt = $handler->prepare($sql);
			$stmt->execute();

these lines make up "execute" method:

	public static function execute($sql) {
		try {
			$handle = self::connect();
			$stmt = $handle->prepare($sql);
			$stmt->execute();
		}
		catch(PDOException $e) {
			self::close();
			trigger_error($e->getMessage(), E_USER_ERROR);
		}
	}

So if i try to replace getAll with this:

	public static function getAll($sql, $style = PDO::FETCH_ASSOC) {
		$result = null;

		try {
			$self::execute($sql);
			$result = $stmt->fetchAll($style);
		}
		catch(PDOException $e) {
			self::close();
			trigger_error($e->getMessage(), E_USER_ERROR);
		}

	return $result;
	}

i get an error:

Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM in E:\PortableApps\WOS\www\ahk_php\lib\Database.class.php on line 41

Why is this happening? Those are the same lines of code. Any idea?

Link to comment
https://forums.phpfreaks.com/topic/114779-really-weird-problem-with-wrapped-pdo/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.