public static function getDB() { if (!isset(self::$db)) { try { self::$db = new PDO(self::$dsn, self::$username, self::$passwd); } catch (PDOException $ex) { $error_message = $ex->getMessage(); include '../errors/datebase_error.php'; exit; } } return self::$db; }
public static function getPublisher($publisher_id) { global $db; $query = 'SELECT * FROM publishers WHERE publisherID = :publisher_id'; try { $statement = $db->prepare($query); $statement->bindValue(':publisher_id', $publisher_id); $statement->execute(); $row = $statement->fetch(); $statement->closeCursor(); $publisher = new Publisher($row['publisherID'], $row['publisherName']); return $publisher; } catch (PDOException $e) { $error_message = $e->getMessage(); DBContext::displayDBError($error_message); } }
function is_array($fname, $lname, $dob) { global $db; $query = 'SELECT userID FROM users WHERE fname = :fname AND lname= :lname AND dob = :dob AND isAdmin = 1'; try { $statement = $db->prepare($query); $statement->bindValue(':fname', $fname); $statement->bindValue(':lname', $lname); $statement->bindValue(':dob', $dob); $statement->execute(); $valid = $statement->rowCount() >= 1; $statement->closeCursor(); } catch (PDOException $e) { $error_message = $e->getMessage(); DBContext::displayDBError($error_message); } return $valid; }
/** * 配置初始化函数 * @param $conf array 配置项如下: * guid_db 可选 * guid_table 可选 * splits 可选 * log_func 可选 * test_mode 可选 * db_pool=>array( * 'db11'=>array( * 'ip'=>'ip', * 'port'=>3306, * 'user'=>'user', * 'pass'=>'pass', * 'charset'=>'charset' * ), * 'db2'=>xxx * .... * ), * 'dbs'=>array( * 'dbname'=>'db1', * 'dbname'=>array('master'=>'db1','slave'=>array('db2','db3')) * ) * @throws \Exception */ static function init($conf) { //check db conf format foreach ($conf['dbs'] as $db => $dbconf) { if (is_string($dbconf)) { if (!isset($conf['db_pool'][$dbconf])) { throw new \Exception('db.ConfError ' . $dbconf . ' no such pool in db_pool'); } } else { if (!isset($dbconf['master']) || !isset($dbconf['slave'])) { throw new \Exception('db.ConfError missing master|slave conf ' . $db); } $master = $dbconf['master']; $slaves = $dbconf['slave']; if (!isset($conf['db_pool'][$master])) { throw new \Exception('db.ConfError ' . $master . ' no such pool in db_pool'); } foreach ($slaves as $slave) { if (!isset($conf['db_pool'][$slave])) { throw new \Exception('db.ConfError ' . $slave . ' no such pool in db_pool'); } } } } DbContext::$db_pool = $conf['db_pool']; DBContext::$dbconf = $conf['dbs']; DbContext::$guidDB = empty($conf['guid_db']) ? null : $conf['guid_db']; DbContext::$guidTable = empty($conf['guid_table']) ? null : $conf['guid_table']; DbContext::$defaultDB = empty($conf['default_db']) ? null : $conf['default_db']; //转换成小写的,免得因为大小写问题比较不成功 DbContext::$splits = empty($conf['splits']) ? array() : $conf['splits']; DbContext::$logFunc = empty($conf['log_func']) ? null : $conf['log_func']; DbContext::$testMode = !empty($conf['test_mode']); DbContext::$longQueryTime = empty($conf['long_query_time']) ? 0 : $conf['long_query_time']; }
/** * * Return all service request within the given filter/s * @param array $filter an array of filters, array index as column filter and array first value as column filter value , array second value as column operator * @return return an array of servicerequest object */ public function getServiceRequest($ordering = array(), $filter = array()) { $where = "WHERE "; $order = " ORDER BY "; $request = array(); if (count($filter) == 0) { $where = ""; } else { foreach ($filter as $key => $value) { if (!array_key_exists(0, $value)) { return $request; } if (!array_key_exists(1, $value)) { return $request; } $val = is_numeric($value[0]) ? $value[0] : "'" . $value[0] . "'"; $where .= $key . " " . $value[1] . " " . $val . " AND "; } $where = substr($where, 0, strlen($where) - 4); } if (count($ordering) == 0) { $order = ""; } else { foreach ($ordering as $key => $value) { $order .= $key . " " . $value; } } $query = "SELECT * FROM servicerequest " . $where . $order; $dbContext = new DBContext(); $resultArr = $dbContext->getAllResult($query);
public static function displayDBError($error_message) { self::$error_msg = $error_message; include 'views/db_error.php'; exit; }
public static function addBook($book) { global $db; $query = 'INSERT INTO books (publisherID, isbn, bookTitle, bookPrice) VALUES (:publisher_id, :isbn, :title, :price)'; try { $statement = $db->prepare($query); $statement->bindValue(':publisher_id', $publisher_id); $statement->bindValue(':isbn', $isbn); $statement->bindValue(':title', $title); $statement->bindValue(':price', $price); $statement->execute(); $statement->closeCursor(); // Get the last product ID that was automatically generated $book_id = $db->lastInsertId(); return $book_id; } catch (PDOException $e) { $error_message = $e->getMessage(); DBContext::displayDBError($error_message); } }
* Student Info: Name=Tsai-Chang Mai, ID=10010 * Subject: CS526(C)_HW5_Spring_2016 * Author: Arvin-tcm * Filename: index.php * Date and Time: Mar 24, 2016 6:14:18 PM * Project Name: TsaiChangMai_10010_CS526A_HW5 */ include_once "models/PageData.php"; $pageData = new PageData(); $pageData->title = "Rest"; $pageData->addCSS('css/layout.css'); $pageData->addCSS('css/navigation.css'); // start session management with a persistent cookie $duration = 60 * 60 * 24 * 7; session_set_cookie_params($duration); session_start(); if (empty($_SESSION['cart'])) { $_SESSION['cart'] = array(); } //connect to database include_once "db/db_context.php"; $db = DBContext::getDB(); $pageData->navigation = (include_once "views/navigation_front.php"); $navigationIsClicked = isset($_GET["controller"]); if ($navigationIsClicked) { $controller = $_GET["controller"]; } else { $controller = "guest"; } $pageData->content = (include_once "controllers/{$controller}/index.php"); include_once "views/page.php";