/** * Gets a connection to the database using the PDO drivers * @return PDO instance of the connection to the database */ public static function DBH() { if (!self::$DBH) { self::$DBH = new PDO("mysql:host=" . config::DBHOST . ";dbname=" . config::DBDATABASE, config::DBUSER, config::DBPASSWORD); } return self::$DBH; }
/** * Returns all the number of matching records found in the database * @param string $query optional formatted SQL query. If omitted, all records from the table will be returned * @param array $params optional array of parameter values to use if the query contains bound parameters * @return integer number of matching records found in the database */ public function getCount($conditions = null, $params = null) { $query = "SELECT COUNT(*) FROM {$this->name}"; if (!empty($conditions)) { $query .= " WHERE {$conditions}"; } $STH = testProject::DBH()->prepare($query); if (!empty($params)) { $STH->execute($params); } else { $STH->execute(); } $result = $STH->fetchColumn(); return $result; }
/** * Returns all the number of matching records found in the database * @param string $query optional formatted SQL query. If omitted, all records from the table will be returned * @param array $params optional array of parameter values to use if the query contains bound parameters * @return integer number of matching records found in the database */ public function getCount($conditions = null, $params = null) { $query = Query::build()->from($this->name)->bind($this->pk, $pk); if ($this->filterByUser) { $query = $query->where('user', '=', Auth::User()->id); } $count = $query->count(); return $count; $query = "SELECT COUNT(*) FROM {$this->name}"; if (!empty($conditions)) { $query .= " WHERE {$conditions}"; } $STH = testProject::DBH()->prepare($query); if (!empty($params)) { $STH->execute($params); } else { $STH->execute(); } $result = $STH->fetchColumn(); return $result; }
public function run($debug = false) { $query = ''; if (count($this->selects) > 0) { //Build a select query $selects = implode(', ', $this->selects); $tables = implode(', ', $this->tables); $query = "SELECT {$selects} FROM {$tables}"; } if (count($this->wheres) > 0) { $wheres = implode(' AND ', $this->wheres); $query .= " WHERE {$wheres}"; } if (count($this->orders) > 0) { $orders = implode(', ', $this->orders); $query .= " ORDER BY {$orders}"; } if ($debug) { var_dump($query); } if ($debug) { var_dump($this->bindings); } $STH = testProject::DBH()->prepare($query); if (count($this->bindings) > 0) { $STH->execute($this->bindings); } else { $STH->execute(); } return $STH; }