Ejemplo n.º 1
0
Archivo: db.php Proyecto: nishad/bmtmgr
function connect()
{
    $dsn = \str_replace('$ROOTDIR', \dirname(__DIR__), \bmtmgr\config\get('db_dsn'));
    $db = new \PDO($dsn);
    $db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC);
    // Do we need to initialize?
    if (\bmtmgr\config\get('allow_init', false)) {
        if (\bmtmgr\config\get('test_force_init', false)) {
            _init($db);
            return $db;
        }
        $init_sql = \file_get_contents(dirname(__DIR__) . '/db_init.sql');
        if (!\preg_match('/INSERT INTO db_version.*VALUES\\s*\\(([0-9]+)\\)/', $init_sql, $matches)) {
            throw new \Exception('Cannot detect version number');
        }
        $newest_version = \intval($matches[1]);
        try {
            $vdata = $db->query('SELECT version FROM db_version');
        } catch (\PDOException $e) {
            _init($db);
            return $db;
        }
        $version = -1;
        foreach ($vdata as $row) {
            $version = $row['version'];
        }
        if ($version < $newest_version) {
            _init($db);
        }
    }
    return $db;
}
Ejemplo n.º 2
0
<?php

define('DATABASE', '/Users/hasan_azimi0/Sites/phpLiteAdmin/db/test.sqlite3');
define('TITLE', 'PHP testing sandbox');
define('VERSION', '1.0.4');
define('HEADER', '../../assets/header.php');
define('BODY', '../../assets/body.php');
define('FOOTER', '../../assets/footer.php');
_init();
main();
page();
function main()
{
    global $G;
    message("PHP testing sandbox (%s) version %s", $G['ME'], VERSION);
    try {
        $db = new PDO('sqlite:' . DATABASE);
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $db->exec('DROP TABLE IF EXISTS t');
        $db->exec('CREATE TABLE t (a, b, c)');
        message('Table t sucessfully created');
        $sth = $db->prepare('INSERT INTO t VALUES (?, ?, ?)');
        $sth->execute(array('a', 'b', 'c'));
        $sth->execute(array(1, 2, 3));
        $sth->execute(array('one', 'two', 'three'));
        $sth = $db->prepare('SELECT * FROM t');
        $sth->setFetchMode(PDO::FETCH_ASSOC);
        $sth->execute();
        foreach ($sth as $row) {
            message('%s, %s, %s', $row['a'], $row['b'], $row['c']);
        }