/**
 * Gets the DB connection
 * @return PDO The DB Connection
 */
function getDatabase()
{
    try {
        $db = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME . ';charset=utf8mb4', DB_USER, DB_PASS);
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
        return $db;
    } catch (Exception $e) {
        showDbError('connect', $e->getMessage());
    }
}
Example #2
0
<?php

/**
 * Redirects to the error handling page
 * @param string $type
 * @return void
 */
function showDbError($type, $msg)
{
    file_put_contents(__DIR__ . '/error_log_mysql', PHP_EOL . (new DateTime())->format('Y-m-d H:i:s') . ' : ' . $msg, FILE_APPEND);
    header('location: error.php?type=db&detail=' . $type);
    exit;
}
// Include config
require_once 'config.php';
// Make Connection
try {
    $db = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME_FF . ';charset=utf8mb4', DB_USER, DB_PASS);
} catch (Exception $e) {
    showDbError('connect', $e->getMessage());
}
$stmt = $db->exec('DELETE FROM collections WHERE user_id = 10');
var_dump($stmt);
// Handle result here ....
//EOF
Example #3
0
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
} catch (Exception $ex) {
    showDbError('connect', $ex->getMessage());
}
/**
 * No action to handle: show our page itself
 * ----------------------------------------------------------------
 */
// Fetch needed data from DB
// @TODO get all items from databases
$items = array();
try {
    if (isset($_GET['topic'])) {
        $resultSet = $db->prepare('SELECT books.id,books.title AS "booktitle",numpages,cover_extension,topics.id AS "topicid",topics.title AS "topictitle",username FROM `books` join users on user_id=users.id join topics on topic_id=topics.id WHERE topics.id=? ORDER BY booktitle ASC;');
        $resultSet->execute(array($_GET['topic']));
    } else {
        $resultSet = $db->query('SELECT books.id,books.title AS "booktitle",numpages,cover_extension,topics.id AS "topicid",topics.title AS "topictitle",username FROM `books` join users on user_id=users.id join topics on topic_id=topics.id ORDER BY booktitle ASC;');
    }
    if ($resultSet) {
        foreach ($resultSet as $rij) {
            array_push($items, $rij);
        }
    }
} catch (Exception $ex) {
    showDbError('get', $ex->getMessage());
}
$loader = new Twig_Loader_Filesystem(__DIR__ . '/templates');
$twig = new Twig_Environment($loader, array('cache' => __DIR__ . '/cache', 'auto_reload' => true));
$tpl = $twig->loadTemplate('index.twig');
echo $tpl->render(array('pageTitle' => 'Index', 'books' => $items, 'user' => isset($_SESSION['user']) ? $_SESSION['user']['username'] : null));
Example #4
0
<?php

/**
 * Redirects to the error handling page
 * @param string $type
 * @return void
 */
function showDbError($type)
{
    // The referrerd page will show a proper error based on the $_GET parameters
    header('location: error.php?type=db&detail=' . $type);
    exit;
}
// Include config
require_once 'config.php';
// Make Connection
try {
    $db = new PDO('mysql:host=' . DB_HOST . ';dbname=does_not_exist;charset=utf8mb4', DB_USER, DB_PASS);
} catch (Exception $e) {
    showDbError('connect');
}
echo 'Connected to the database';
// ... your query magic here
//EOF
Example #5
0
 * Includes
 * ----------------------------------------------------------------
 */
// config & functions
require_once 'includes/config.php';
require_once 'includes/functions.php';
/**
 * Database Connection
 * ----------------------------------------------------------------
 */
// @TODO
try {
    $db = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME . ';charset=utf8mb4', DB_USER, DB_PASS);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (Exception $aight) {
    showDbError($aight->getMessage(), "database error");
}
/**
 * Initial Values
 * ----------------------------------------------------------------
 */
$priorities = array('low', 'normal', 'high');
// The possible priorities of a todo
$formErrors = array();
// The encountered form errors
$id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
// The passed in id of the todo
$what = isset($_POST['what']) ? $_POST['what'] : '';
// The todo that was sent in via the form
$priority = isset($_POST['priority']) ? $_POST['priority'] : 'low';
// The priority that was sent in via the form