/** * * @return PDO */ public static function getConnection() { if (!isset(self::$con)) { $dsn = 'mysql:host=' . self::$server . ';dbname=' . self::$database; self::$con = new PDO($dsn, self::$user, self::$password); self::$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } return self::$con; }
<?php require_once '../php/DB_connection.php'; $dbh = DB_connection::getConnection(); $statement = $_GET['query']; $sth = $dbh->prepare($statement); $sth->execute(); if (strpos($statement, "DELETE") === false) { $data = $sth->fetchAll(PDO::FETCH_NUM); echo json_encode($data); }
<?php require_once 'Bootstrap.php'; $connection = new DB_connection(); if (!empty($_POST)) { // collect action to determine what to call $action = $_POST['action']; // collect dataType to return $dataType = $_POST['dataType']; // no longer needed, and don't want it added to queries unset($_POST['action']); unset($_POST['dataType']); switch ($action) { case 'add': case 'insert': case 'save': case 'update': $confirmation = $connection->save($_POST); break; case 'delete': $confirmation = $connection->delete($_POST); break; default: // get $confirmation = $connection->get($_POST); } // return JSON results if specified if ($dataType && strtolower($dataType) === 'json') { $confirmation = json_encode($confirmation); } die($confirmation);
function remove($id) { $db = new DB_connection(); return $db->delete(config::TABLENAME, $id); }