public static function getInviteById($id) { $statement = DB::getInstance()->prepare("SELECT * FROM invite WHERE `id`=? LIMIT 0,1"); $statement->bindValue(1, $id, DB::PARAM_STR); $statement->execute(); return $statement->fetchObject(__CLASS__); }
public static function getUserId($username, $password) { $result = Database::getInstance()->prepare("SELECT id FROM `users` where `username` = :username AND `password`=:password OR `email`=:username AND `password`=:password"); $result->execute(array(':username' => $username, ':password' => $password)); $row = $result->fetch(PDO::FETCH_ASSOC); return $row['id']; }
public static function init() { $stn = DB::getInstance()->prepare("SELECT k, v FROM options"); $stn->execute(); $opt = $stn->fetchAll(DB::FETCH_UNIQUE | DB::FETCH_COLUMN); // $GLOBALS['OPTIONS'] = $opt; self::$list = $opt; return $opt; }
/** * Create a new PageData object. * @param string $tableName Target table name * @param string $extras Such as where statement or order statement * @param array $column Column names needs to be fetch */ public function __construct($tableName, $extras = '', $column = array('*')) { $columns = '`' . implode('`, `', $column) . '`'; $this->countQuery = Database::getInstance()->prepare("SELECT COUNT(*) FROM `{$tableName}` {$extras}"); $this->query = Database::getInstance()->prepare("SELECT {$columns} FROM `{$tableName}` {$extras} LIMIT :pageDataStart,:pageDataRPP"); if ($_GET['page']) { $this->setPage($_GET['page']); } }
public function save($mode = self::SAVE_AUTO) { $map = array(); $reflection = new ReflectionObject($this); $reflectionProp = $reflection->getProperties(ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PUBLIC); foreach ($reflectionProp as $property) { if (strpos($property->getDocComment(), '@ignore')) { continue; } $propertyName = $property->getName(); if ($propertyName == 'primaryKey') { continue; } if ($property->isProtected()) { $property->setAccessible(true); } $propertyValue = $property->getValue($this); $map[$propertyName] = $propertyValue; } $primaryKey = $this->getPrimaryKeyName($reflection); $identifier = $map[$primaryKey]; unset($map[$primaryKey]); $tableName = $this->getTableName($reflection); if ($mode == self::SAVE_UPDATE || $identifier && $mode != self::SAVE_INSERT) { $sql = "UPDATE `{$tableName}` SET "; foreach ($map as $key => $value) { $sql .= "`{$key}` = :{$key},"; } $sql = rtrim($sql, ','); $sql .= " WHERE {$primaryKey} = :id"; $statement = Database::getInstance()->prepare($sql); $statement->bindValue(':id', $identifier); foreach ($map as $key => $value) { $statement->bindValue(":{$key}", $value); } } else { $sql = "INSERT INTO `{$tableName}` SET "; foreach ($map as $key => $value) { $sql .= "`{$key}` = :{$key},"; } $sql = rtrim($sql, ','); $statement = Database::getInstance()->prepare($sql); foreach ($map as $key => $value) { $statement->bindValue(":{$key}", $value); } } $statement->execute(); if (!$identifier) { $insertId = Database::getInstance()->lastInsertId(); if ($insertId) { $reflection->getProperty($primaryKey)->setValue($this, $insertId); } } }
/** * Migrate current database * @param $dropTable bool drop the table */ public function execute($dropTable = false) { $this->database = Database::getInstance(); $modelDir = "Application/Model"; $file = opendir($modelDir); // there is fileName while (($fileName = readdir($file)) !== false) { if (substr($fileName, -4) == ".php") { $this->migrateTable($modelDir . "/" . $fileName, $dropTable); } } }
public static function setNextRun($cronId, $step) { $inTransaction = DB::getInstance()->inTransaction(); if (!$inTransaction) { DB::getInstance()->beginTransaction(); } $st = DB::sql("UPDATE cron SET nextrun=? WHERE id=?"); $st->bindValue(1, $step, DB::PARAM_INT); $st->bindValue(2, $cronId, DB::PARAM_STR); $st->execute(); if (!$inTransaction) { DB::getInstance()->commit(); } }
public function destroy() { $inTransaction = DB::getInstance()->inTransaction(); if (!$inTransaction) { DB::getInstance()->beginTransaction(); } $st = DB::sql("UPDATE card SET status=0 WHERE card=:card"); // 失效卡 $st->bindValue(":card", $this->card, DB::PARAM_STR); $flag = $st->execute(); if (!$inTransaction) { DB::getInstance()->commit(); } return $flag; }
/** * Execute a PDO prepare with execute * * @param $query * The query to execute * @param array $params * The query parameters * @return An array with the results */ public static function execute($query, $params = []) { // Preparing the query with the database instance $results = Database::getInstance()->prepare($query); // Executing the query with the given parameters $results->execute($params); // Fetching the results $results = $results->fetchAll(\PDO::FETCH_OBJ); // Checking them if (count($results) < 2) { $results = current($results); } // Returning them return $results; }
public function run() { $resetDate = '1'; $date = date("d", time()); if ($date == $resetDate) { $inTransaction = DB::getInstance()->inTransaction(); if (!$inTransaction) { DB::getInstance()->beginTransaction(); } $st = DB::sql("UPDATE member SET flow_up=0, flow_down=0 WHERE `enable`=1 AND `plan`!='Z'"); $st->execute(); if (!$inTransaction) { DB::getInstance()->commit(); } } return false; }
public function insert() { $reflection = new ReflectionObject($this); $map = $this->getTableMap($reflection); $primaryKey = $this->getPrimaryKeyName($reflection); $tableName = $this->getTableName($reflection); $sql = "INSERT INTO `{$tableName}` SET "; foreach ($map as $key => $value) { $sql .= "`{$key}` = :{$key},"; } $sql = rtrim($sql, ','); $statement = Database::getInstance()->prepare($sql); foreach ($map as $key => $value) { $statement->bindValue(":{$key}", $value); } $statement->execute(); $insertId = Database::getInstance()->lastInsertId(); if ($insertId) { $reflection->getProperty($primaryKey)->setValue($this, $insertId); } }
<?php session_start(); spl_autoload_register(function ($class) { $classPath = str_replace("\\", "/", $class); require_once $classPath . '.php'; }); require_once 'core/App.php'; \Core\Database::SetInstance(\Config\DatabaseConfig::DB_INSTANCE, \Config\DatabaseConfig::DB_DRIVER, \Config\DatabaseConfig::DB_USER, \Config\DatabaseConfig::DB_PASS, \Config\DatabaseConfig::DB_NAME, \Config\DatabaseConfig::DB_HOST); $app = new \Core\App(\Core\Database::getInstance(\Config\DatabaseConfig::DB_INSTANCE)); function loadTemplate($templateName, $data = null) { require_once 'templates/' . $templateName . '.php'; }
/** * Delete node * @param $nodeId * @return bool */ public static function deleteNode($nodeId) { $inTransaction = DB::getInstance()->inTransaction(); if (!$inTransaction) { DB::getInstance()->beginTransaction(); } $statement = DB::getInstance()->prepare("DELETE FROM node WHERE id=:id"); $statement->bindValue(':id', $nodeId, DB::PARAM_INT); $result = $statement->execute(); if (!$inTransaction) { DB::getInstance()->commit(); } return $result; }
/** * @return UserCollection * @throws \Exception */ public function pagination($pageNum, $count) { $param1 = (int) $pageNum; $param2 = (int) $count; $this->placeholders[] = $pageNum; $this->placeholders[] = $count; $db = Database::getInstance('app'); $this->query = "SELECT * FROM user" . $this->where . " ORDER BY createdAt DESC LIMIT {$param1},{$count};"; $result = $db->prepare($this->query); $result->execute($this->placeholders); $collection = []; foreach ($result->fetchAll() as $entityInfo) { $entity = new User($entityInfo['username'], $entityInfo['password'], $entityInfo['registerDate'], $entityInfo['emailVerified'], $entityInfo['email'], $entityInfo['createdAt'], $entityInfo['updatedAt'], $entityInfo['id']); $collection[] = $entity; self::$selectedObjectPool[] = $entity; } return new UserCollection($collection); }
/** * @return SdfCollection * @throws \Exception */ public function pagination($pageNum, $count) { $param1 = (int) $pageNum; $param2 = (int) $count; $this->placeholders[] = $pageNum; $this->placeholders[] = $count; $db = Database::getInstance('app'); $this->query = "SELECT * FROM sdf" . $this->where . " ORDER BY createdAt DESC LIMIT {$param1},{$count};"; $result = $db->prepare($this->query); $result->execute($this->placeholders); $collection = []; foreach ($result->fetchAll() as $entityInfo) { $entity = new Sdf($entityInfo['id']); $collection[] = $entity; self::$selectedObjectPool[] = $entity; } return new SdfCollection($collection); }
private static function insert(IdentitySessionModel $model) { $db = Database::getInstance('app'); $query = "INSERT INTO usersessions (userid,session,loginDate) VALUES (:userid, :session, :loginDate);"; $result = $db->prepare($query); $result->execute([':userid' => $model->getUserId(), ':session' => $model->getSession(), ':loginDate' => $model->getLoginDate()]); $model->setId($db->lastId()); //d($result->error()); }
/** * @return ArticleCollection * @throws \Exception */ public function pagination($pageNum, $count) { $param1 = (int) $pageNum; $param2 = (int) $count; $this->placeholders[] = $pageNum; $this->placeholders[] = $count; $db = Database::getInstance('app'); $this->query = "SELECT * FROM articles" . $this->where . " ORDER BY createdAt DESC LIMIT {$param1},{$count};"; $result = $db->prepare($this->query); $result->execute($this->placeholders); $collection = []; foreach ($result->fetchAll() as $entityInfo) { $entity = new Article($entityInfo['title'], $entityInfo['introNewsData'], $entityInfo['newsData'], $entityInfo['picture'], $entityInfo['thumbnail'], $entityInfo['createdAt'], $entityInfo['category_id'], $entityInfo['isPosted'], $entityInfo['id']); $collection[] = $entity; self::$selectedObjectPool[] = $entity; } return new ArticleCollection($collection); }
private static function insert(IdentityUserModel $model) { $db = Database::getInstance('app'); $query = "INSERT INTO users (username,password,fullname) VALUES (:username, :password, :fullname);"; $result = $db->prepare($query); $result->execute([':username' => $model->getUsername(), ':password' => $model->getPassword(), ':fullname' => $model->getFullname()]); $model->setId($db->lastId()); //d($result->error()); }
/** * 后端统计 <b>在线时间区间</b> * * @param int $type * @return int */ public static function onlineUsage($type = 0) { $querySQL = "SELECT "; // sql... $stn = DB::getInstance()->prepare($querySQL); $stn->execute(); $count = $stn->fetch(DB::FETCH_NUM); return $count[0] != null ?: 0; }
define('DX_ROOT_DIR', dirname(__FILE__) . '/'); define('DX_ROOT_PATH', basename(dirname(__FILE__)) . '/'); $request = $_SERVER['REQUEST_URI']; $request_home = '/' . DX_ROOT_PATH; $controller = 'master'; $method = 'index'; $param = array(); include_once 'config/db.php'; include_once 'core/database.php'; include_once "controllers/master.php"; include_once "controllers/products.php"; if (!empty($request)) { if (0 === strpos($request, $request_home)) { $request = substr($request, strlen($request_home)); $components = explode('/', $request, 3); if (1 < count($components)) { list($controller, $method) = $components; if (isset($components[2])) { $param = $components[2]; } include_once 'controllers/' . $controller . '.php'; } } } $controller_class = '\\controllers\\' . ucfirst($controller) . '_Controller'; $instance = new $controller_class(); if (method_exists($instance, $method)) { call_user_func_array(array($instance, $method), array($param)); } $db_object = \core\Database::getInstance(); var_dump($db_object);
public static function createidentityTables() { $db = Database::getInstance('app'); $queryGetTables = "SHOW TABLES"; $dbNames = $db->prepare($queryGetTables); $dbNames->execute(); $tableNames = $dbNames->fetchAll(); if (count($tableNames) <= 0) { $queryUserTable = "CREATE TABLE Users(id int NOT NULL AUTO_INCREMENT,username varchar(255),password varchar(255),fullname varchar(255),PRIMARY KEY (id))ENGINE=InnoDB;"; $queryIndex = "ALTER TABLE Users ADD INDEX (id);"; $result = $db->prepare($queryUserTable); $result->execute(); //ADD AUTOINCREMENTED INDEX $queryAutoincrement = "ALTER TABLE Users AUTO_INCREMENT = 1"; $result = $db->prepare($queryAutoincrement); $result->execute(); //ADD ADMINISTRATOR USER $queryAddAdministrator = "INSERT INTO users(username,password,fullname) VALUES ('admin', 'admin', 'ADMINISTRATOR');"; $result = $db->prepare($queryAddAdministrator); $result->execute(); //CREATE TABLE ROLES $queryRoleTable = "CREATE TABLE Roles(id int NOT NULL AUTO_INCREMENT,Rolename varchar(255),PRIMARY KEY (id))ENGINE=InnoDB;;"; $result = $db->prepare($queryRoleTable); $result->execute(); //ADD ADMINISTRATOR ROLE $queryAddAdministrator = "INSERT INTO roles(Rolename) VALUES ('administrator');"; $result = $db->prepare($queryAddAdministrator); $result->execute(); //ADD EXTENDED USER ROLE $queryAddModerator = "INSERT INTO roles(Rolename) VALUES ('moderator');"; $result = $db->prepare($queryAddModerator); $result->execute(); //ADD ADMINISTRATOR ROLE $queryAddRegistered = "INSERT INTO roles(Rolename) VALUES ('registered');"; $result = $db->prepare($queryAddRegistered); $result->execute(); //CREATE MANY TO MANY TABLE USER-ROLES $QueryUserRoleTable = "CREATE TABLE userRole(id int NOT NULL AUTO_INCREMENT,userid int, roleid int,PRIMARY KEY (id))ENGINE=InnoDB;;"; $result = $db->prepare($QueryUserRoleTable); $result->execute(); //ADD FOREIGN KEY $foreightKeyConstraintQuery = "ALTER TABLE userRole ADD CONSTRAINT FK_UserRoles_Userid FOREIGN KEY (userid)REFERENCES Users(id);"; $result = $db->prepare($foreightKeyConstraintQuery); $result->execute(); //ADD FOREIGN KEY $foreightKeyConstraintQuery2 = "ALTER TABLE userRole ADD CONSTRAINT FK_UserRoles_Roleid FOREIGN KEY (roleid)REFERENCES Roles(id);"; $result = $db->prepare($foreightKeyConstraintQuery2); $result->execute(); //ADD ADMINISTRATOR ROLE $queryAddAdministratorToAdminRole = "INSERT INTO userrole(userid,roleid) VALUES (1,1);"; $result = $db->prepare($queryAddAdministratorToAdminRole); $result->execute(); //CREATE USERSESSIONS TABLE $QueryUserSessionTable = "CREATE TABLE userSessions(id int NOT NULL AUTO_INCREMENT,userid int NOT NULL, session int,loginDate datetime,PRIMARY KEY (id))ENGINE=InnoDB;;"; $result = $db->prepare($QueryUserSessionTable); $result->execute(); //ADD FOREIGN KEY $foreightKeyConstraintQuery = "ALTER TABLE userSessions ADD CONSTRAINT FK_UserSession_Userid FOREIGN KEY (userid)REFERENCES Users(id);"; $result = $db->prepare($foreightKeyConstraintQuery); $result->execute(); var_dump($result->error()); } }
/** * @param $item * * @return int number of affected rows */ public function delete($item) { $itemId = $item; if (is_object($item)) { $itemId = $item->id; } $query = "DELETE FROM " . $this->getTableName() . " WHERE id = ?"; return Database::getInstance()->executeUpdate($query, array($itemId)); }
/** * @param null $page * @param null $count * @return object * @throws \Exception */ public function get($page = null, $count = null) { $query = $this->query . $this->where; if (!is_null($page) && is_null($count)) { $query .= " LIMIT {$page};"; } else { if (!is_null($page) && !is_null($count)) { $page = $page * $count; $query .= " LIMIT {$page},{$count};"; } } $db = Database::getInstance('app'); $result = $db->prepare($query); $result->execute(); $customObject = $result->fetchAll(); $outPutObject = (object) array("results" => $customObject); return $outPutObject; }
<?php use Core\Database; use Config\DatabaseConfig; use Core\App; session_start(); spl_autoload_register(function ($class) { $classPath = str_replace('\\', '/', $class . '.php'); require_once $classPath; }); require_once 'core/App.php'; Database::setInstance(DatabaseConfig::DB_INSTANCE_NAME, DatabaseConfig::DB_DRIVER, DatabaseConfig::DB_USERNAME, DatabaseConfig::DB_PASSWORD, DatabaseConfig::DB_NAME, DatabaseConfig::DB_HOST); $app = new App(Database::getInstance(DatabaseConfig::DB_INSTANCE_NAME)); function loadTemplate($templateName, $data = null) { require_once 'templates/' . $templateName . '.php'; }
/** * 停止用户 */ public function stop() { $inTransaction = DB::getInstance()->inTransaction(); if (!$inTransaction) { DB::getInstance()->beginTransaction(); } $stn = DB::sql("UPDATE member SET `enable` = 0 WHERE uid=?"); $stn->bindValue(1, $this->uid, DB::PARAM_INT); $stn->execute(); if (!$inTransaction) { DB::getInstance()->commit(); } }
public function setup() { $this->database = Database::getInstance(); $this->customerRepo = new CustomerRepository($this->database); }
/** * Find one * @return first element */ public function first() { $result = null; $this->paginate(0, 1); $this->buildQuery(); $resultSet = Database::getInstance()->executeQuery($this->query, "Application\\Model\\" . $this->entityClass); if (sizeof($resultSet) > 0) { $result = $resultSet[0]; } return $result; }
<?php /** * KK-Framework * Author: kookxiang <*****@*****.**> */ if (PHP_SAPI != 'cli') { exit('This file could not be access directly.'); } define('ROOT_PATH', dirname(__FILE__) . DIRECTORY_SEPARATOR); define('LIBRARY_PATH', ROOT_PATH . 'Library/'); define('DATA_PATH', ROOT_PATH . 'Data/'); @ini_set('display_errors', 'on'); require ROOT_PATH . 'Package/autoload.php'; Core\Error::registerHandler(); @(include DATA_PATH . 'Config.php'); $statement = \Core\Database::getInstance()->query('select database()'); $statement->execute(); return array('paths' => array('migrations' => ROOT_PATH . 'Migrations'), 'environments' => array('default_database' => 'current', 'current' => array('name' => $statement->fetchColumn(), 'connection' => \Core\Database::getInstance())));