diff --git a/system/model.php b/system/model.php index 04503ca..802c396 100644 --- a/system/model.php +++ b/system/model.php @@ -1,62 +1,71 @@ connection = mysql_pconnect($config['db_host'], $config['db_username'], $config['db_password']) or die('MySQL Error: '. mysql_error()); - mysql_select_db($config['db_name'], $this->connection); + + try{ + $dsn = "mysql:dbname={$config['db_name']};host={$config['db_host']}"; + $this->connection = parent::__construct($dsn, $config['db_username'], $config['db_password']); + parent::setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + } + catch(PDOException $e){ + return $e->getMessage(); + } } - public function escapeString($string) - { + //deprecated + public function escapeString($string){ return mysql_real_escape_string($string); } - public function escapeArray($array) - { + //deprecated + public function escapeArray($array){ array_walk_recursive($array, create_function('&$v', '$v = mysql_real_escape_string($v);')); return $array; } - public function to_bool($val) - { + public function to_bool($val){ return !!$val; } - public function to_date($val) - { + public function to_date($val){ return date('Y-m-d', $val); } - public function to_time($val) - { + public function to_time($val){ return date('H:i:s', $val); } - public function to_datetime($val) - { + public function to_datetime($val){ return date('Y-m-d H:i:s', $val); } - public function query($qry) - { - $result = mysql_query($qry) or die('MySQL Error: '. mysql_error()); - $resultObjects = array(); + public function query($qry, $params=array()){ + try{ + $pdo = $this->prepare($qry); + $pdo->execute($params); - while($row = mysql_fetch_object($result)) $resultObjects[] = $row; + return $pdo->fetchAll(PDO::FETCH_OBJ); + } + catch(PDOException $e){ + return $e->getMessage(); + } - return $resultObjects; } - public function execute($qry) - { - $exec = mysql_query($qry) or die('MySQL Error: '. mysql_error()); - return $exec; + public function execute($qry, $params=array()){ + try{ + $pdo = $this->prepare($qry); + $pdo->execute($params); + return $pdo->rowCount(); + } + catch(PDOException $e){ + return $e->getMessage(); + } } }