/** * Erstellt ein PDO Objekt * * @return PDO pdo */ public static function getPDO() { if (self::$pdo == null) { $config = Config::getArray(); if (!isset($config["database"])) { Helpers::fatalError("No database configuration found."); } $host = ''; if (isset($config['database']['host'])) { $host = 'host=' . $config['database']['host'] . ';'; } $port = ''; if (isset($config['database']['port'])) { $port = 'port=' . $config['database']['port'] . ';'; } $socket = ''; if (isset($config['database']['socket'])) { $socket = 'unix_socket=' . $config['database']['socket'] . ';'; } $dsn = 'mysql:dbname=' . $config["database"]["name"] . ';' . $host . $port . $socket . 'charset=utf8;'; try { $pdo = new PDO($dsn, $config["database"]["user"], $config["database"]["password"], array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); } catch (PDOException $e) { Helpers::fatalError("Could no connect to the database: " . $e->getMessage()); } self::$pdo = $pdo; } return self::$pdo; }
public static function getDb() { if (!isset(self::$pdo)) { self::$pdo = new Pdo(Connection::$connectionString, Connection::$username, Connection::$password); } return self::$pdo; }
public static function getInstance() { if (is_null(self::$pdo)) { self::$pdo = new PDO(sprintf('%s:host=%s; port=%d; dbname=%s; charset=utf8;', 'mysql', 'localhost', 3306, 'cat'), 'cat', 'cat'); self::$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); } return self::$pdo; }
public static function init() { if (self::$pdo === null) { self::$pdo = new \PDO('mysql:host=127.0.0.1;dbname=testdb;charset=utf8', 'user', 'login'); } if (self::$pdo instanceof \PDO) { return true; } return false; }
/** * Connects to database * All the parameters could also be set in config file: * * 'db_dsn' => 'mysql:dbname=bakedcarrot;host=localhost', * 'db_username' => 'db_user', * 'db_password' => 'db_password', * 'db_charset' => 'utf8', * * @static * @param null $dsn DSN in PDO format * @param null $username username to connect to database server * @param null $password connection password * @param string $charset default character set * @return void * @throws BakedCarrotDbException */ public static function connect($dsn = null, $username = null, $password = null, $charset = 'utf8') { if (!is_null(self::$pdo) && !$dsn) { return; } elseif (!is_null(self::$pdo) && $dsn) { self::$pdo = null; } if (!$dsn) { if (!Config::checkVar('db_dsn')) { throw new BakedCarrotDbException('Database access parameters are not properly configured'); } $dsn = Config::getVar('db_dsn'); $username = Config::getVar('db_username'); $password = Config::getVar('db_password'); $charset = Config::getVar('db_charset', 'utf8'); } self::$pdo = new DbPDO($dsn, $username, $password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::MYSQL_ATTR_INIT_COMMAND => 'set names ' . $charset, PDO::ATTR_AUTOCOMMIT => true)); Log::out(__METHOD__ . ' Connected to: "' . $dsn . '"', Log::LEVEL_DEBUG); }
/** * Actually makes a connection to the database (by creating a PDO instance) * @param array $modifications * The modifications to the connection info, if any. * Can contain the keys "dsn", "username", "password", "driver_options" * They are used in constructing the PDO object. */ function pdoConnect($modifications = array()) { if ($this->pdo) { return; } if (!isset($modifications)) { $modifications = array(); } $conn_name = $this->conn_name; $conn_info = Db::getConnection($conn_name); if (empty($conn_info)) { throw new Exception("Database connection \"{$conn_name}\" wasn't registered with Db.", -1); } if (!isset($conn_info['driver_options'])) { $conn_info['driver_options'] = null; } $dsn = isset($modifications['dsn']) ? $modifications['dsn'] : $conn_info['dsn']; $username = isset($modifications['username']) ? $modifications['username'] : $conn_info['username']; $password = isset($modifications['password']) ? $modifications['password'] : $conn_info['password']; $driver_options = isset($modifications['driver_options']) ? $modifications['driver_options'] : $conn_info['driver_options']; $this->pdo = Db::pdo($dsn, $username, $password, $driver_options); $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); }
<?php session_start(); require_once $_SERVER['DOCUMENT_ROOT'] . '/Db.php'; $db = Db::pdo(); print_r($_SESSION); $email = $_SESSION['email']; $statement = $db->prepare("UPDATE user_details SET login_state = '0' WHERE email = '{$email}'"); $statement->execute(); $id = $_SESSION['id']; $statement = $db->prepare("UPDATE user_details SET login_state = '0' WHERE id = '{$id}'"); $statement->execute(); session_unset(); header('Location: http://www.tradeyokickz.com/');
/** * insert or update modified object data into self::$table and any associated metadata * * @return void */ public function save() { if (empty($this->changed)) { return; } $data = array_intersect_key($this->data, $this->changed); // use proper sql NULL for values set to php null foreach ($data as $key => $value) { if ($value === null) { $data[$key] = new PlainSql('NULL'); } } if ($this->id) { $query = 'update `' . static::$table . '` set `' . implode('` = ?, `', array_keys($data)) . '` = ? where `' . static::$pk . '` = ' . $this->id . ' limit 1'; } else { $query = 'insert into `' . static::$table . '` (`' . implode('`,`', array_keys($data)) . "`) values (" . rtrim(str_repeat('?,', count($data)), ',') . ")"; } Db::execute($query, array_values($data)); if ($this->id === null) { $this->id = Db::pdo()->lastInsertId(); } $this->meta->{'set' . Builder::camelCase(static::$meta_field)}($this->id)->save(); $this->hydrate(self::one(array(static::$pk => $this->id))->toArray()); }
self::$fails++; return self::log(" Failed!\n"); } self::log(" Success.\n"); } private static function log($message) { echo $message; } } try { $db = mysql_connect(DbConfig::HOST, DbConfig::USER, DbConfig::PASSWORD); mysql_query("drop database if exists {$dbname}", $db); mysql_query("create database {$dbname}", $db); mysql_close($db); Db::pdo(); OrmTest::assertTrue("Creating test database {$dbname}.", true); } catch (Exception $e) { throw new OrmTestException('Unable to create test database. Probably missing proper config.php or a permissions issue. (' . $e->getMessage() . ')'); } try { Db::execute("CREATE TABLE `{$dbname}`.`user` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n `name` varchar(255) NOT NULL,\n `email` varchar(255) NOT NULL,\n `active` tinyint(4) NOT NULL,\n `created_at` datetime NOT NULL,\n PRIMARY KEY (`id`)\n ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"); Db::execute("CREATE TABLE `{$dbname}`.`phone_number` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n `user_id` int(11) NOT NULL,\n `number` varchar(255) NOT NULL,\n `type` varchar(255) NOT NULL,\n `location` varchar(255),\n PRIMARY KEY (`id`)\n ) ENGINE=InnoDB DEFAULT CHARSET=utf8"); Db::execute("CREATE TABLE `{$dbname}`.`post` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n `user_id` int(11) NOT NULL,\n `title` varchar(255) NOT NULL,\n `body` text NOT NULL,\n PRIMARY KEY (`id`)\n ) ENGINE=InnoDB DEFAULT CHARSET=utf8"); Db::execute("CREATE TABLE `{$dbname}`.`post_meta` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n `post_id` int(11) NOT NULL,\n `key` varchar(255) NOT NULL,\n `val` varchar(255) NOT NULL,\n PRIMARY KEY (`id`)\n ) ENGINE=InnoDB DEFAULT CHARSET=utf8"); Db::execute("CREATE TABLE `{$dbname}`.`orphan` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n `name` varchar(255) NOT NULL,\n `age` varchar(255) NOT NULL,\n PRIMARY KEY (`id`)\n ) ENGINE=InnoDB DEFAULT CHARSET=utf8"); OrmTest::assertTrue("Creating test database tables.", true); } catch (Exception $e) { throw new OrmTestException('Unable to create test tables. Probably a permissions issue. (' . $e->getMessage() . ')'); } try {