public static function Init() { $config = DB::$config; DB::$handler = @mysql_pconnect($config['host'], $config['user'], $config['pass']) or DB::DBdie('Error connecting to MySQL: ' . mysql_error() . "\n"); @mysql_select_db($config['db'], DB::$handler) or DB::DBdie('Error selecting MySQL database: ' . mysql_error() . "\n"); mysql_query('SET AUTOCOMMIT=0', DB::$handler); }
private static function connect($info) { if (self::$handler === false) { try { $options = array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES " . $info['charset'] ?: 'utf8'); self::$handler = new PDO('mysql:' . $info['host'] . '; port=' . $info['port'] . '; dbname=' . $info['database'], $info['username'], $info['password'], $options); // set pdo error mode silent self::$handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT); // If you want to Show Class exceptions on Screen, Uncomment below code self::$handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Use this setting to force PDO to either always emulate prepared statements (if TRUE), // or to try to use native prepared statements (if FALSE). self::$handler->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); // set default pdo fetch mode as fetch assoc self::$handler->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); } catch (PDOException $e) { self::printError("ERROR in establish connection: " . $e->getMessage()); } } else { self::printError('Already connected to DB!'); } }
/** * Try to delete data * * @dataProvider people_provider */ public function test_delete($people) { $handler = DB::handler('phpunit'); $handler->run('delete from `people` where `id` = ?', array(1)); // check the count $this->assertEquals(count($people) - 1, count($handler->fetch('select * from `people`', array()))); }
/** * DB::handler tests */ public function test_handler() { $this->assertTrue(DB::handler() instanceof \DB\Handler); $this->assertTrue(DB::handler('phpunit') instanceof \DB\Handler); }