protected function getPdoDriver() { $pdo = new PHPDataObject('sqlite::memory:'); $pdo->query("\n CREATE TABLE IF NOT EXISTS user(\n id_user INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\n login VARCHAR(20) UNIQUE,\n name VARCHAR(50),\n email VARCHAR(50),\n password VARCHAR(50)\n );\n "); $data = [['duodraco', 'Anderson Casimiro', '*****@*****.**', '********'], ['fulano', 'Fulano de Tal', '*****@*****.**', 'f*******'], ['beltrano', 'Beltrano da Silva', '*****@*****.**', 'b*******'], ['beltrano2', 'Beltrano da Silva', '*****@*****.**', 'b*******'], ['gringo', 'John Doe', '*****@*****.**', 'j*******']]; foreach ($data as $each) { $pdo->query("\n INSERT INTO user (login, name, email, password)\n VALUES ('{$each[0]}','{$each[1]}','{$each[2]}','{$each[3]}');\n "); } $driver = new Pdo($pdo); return $driver; }
/** * @see Yun_Db_Adapter_Interface::query() */ public function query($sql) { if (null === $this->pdo) { return false; } $re = $this->pdo->query($sql); if ($re instanceof PDOStatement) { $re = $re->fetchAll(PDO::FETCH_ASSOC); } elseif (false === $re) { $this->setPdoError(); } return $re; }
/** * {@inheritdoc} */ public function restore($dumpTargetFile, array $ignoreTables = []) { $connection = new Pdo("mysql:host={$this->host};port=3306", $this->username, $this->password); $connection->query("USE {$this->database}"); foreach ($connection->query('SHOW TABLES')->fetchAll(PDO::FETCH_ASSOC) as $table) { $table = array_pop($table); if (in_array($table, $ignoreTables)) { continue; } $connection->query("DROP TABLE `{$table}`")->execute(); } $sql = file_get_contents($dumpTargetFile); $connection->prepare($sql)->execute(); }
/** * Destroys the database and user for the current application. * @return void */ private function destroyMySQL() { $this->appNameRequired(); if ($this->mysql->query('SHOW DATABASES LIKE \'db_' . $this->appname . '\';')->fetchObject()) { $this->writeln('Detected a Application MySQL user and database...'); $this->mysql->exec('DROP DATABASE IF EXISTS `db_' . $this->appname . '`;'); $this->mysql->exec('DROP USER \'' . $this->appname . '\'@\'' . $this->conf->mysql->confrom . '\';'); $this->mysql->exec('FLUSH PRIVILEGES;'); } }
} catch (PDOException $e) { die('SQL执于失败:' . $r . '. ' . $e->getMessage()); } } } } //更新系统版本号 $version = (include 'data/upgrade.php'); $sql = "INSERT INTO {$_SESSION['config']['prefix']}cloud (uid,username,webname,AppSecret,versionCode,releaseCode,createtime)\n\t\tVALUES(0,'','','','{$version['versionCode']}','{$version['releaseCode']}',0)"; try { $pdo->exec($sql); } catch (PDOException $e) { die('SQL执于失败:' . $sql . '. ' . $e->getMessage()); } //设置管理员帐号 $user = $pdo->query("select * from {$_SESSION['config']['prefix']}user where uid=1"); $row = $user->fetchAll(PDO::FETCH_ASSOC); $password = md5($_SESSION['config']['upassword'] . $row[0]['security']); $pdo->exec("UPDATE {$_SESSION['config']['prefix']}user SET password='******' WHERE uid=1"); //修改配置文件 file_put_contents('data/database.php', '<?php return [];?>'); $data = array_merge(include 'system/config/database.php', $_SESSION['config']); file_put_contents('data/database.php', '<?php return ' . var_export($data, TRUE) . ';?>'); header('Location:?a=finish'); } if ($action == 'finish') { //清除运行数据 foreach (glob('data/*') as $f) { if (!in_array(basename($f), ['database.php', 'upgrade.php'])) { @unlink($f); }
* Seriously though, Silex was an obvious choice as the website is quite simple * at the moment. We might want to migrate to something else when the website gets * bigger, but for now, this works like a charm! */ $config = (require __DIR__ . '/../config.php'); $db = new Pdo($config['database']['dsn'], $config['database']['username'], $config['database']['password']); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); /** * Gets the recent pastes from the database. * * @param PDO $db * @return array */ $getRecent = function ($db) { $stmt = $db->query('SELECT paste_id, name, added FROM paste ORDER BY added DESC LIMIT 5 OFFSET 0'); $stmt->execute(); $recent = $stmt->fetchAll(); return $recent !== false ? $recent : array(); }; /** * Gets the details of a paste from the database. * * @param PDO $db * @param string $id * @return array */ $getPaste = function ($db, $id) { $stmt = $db->prepare('SELECT * FROM paste WHERE paste_id = :id'); $stmt->execute(array('id' => $id)); return $stmt->fetch();
private function connect_pdo_sqlsrv($options, $sql) { if (isset($options['server']) && isset($options['user']) && isset($options['password']) && isset($options['database']) && isset($options['charset'])) { try { $pdo = new Pdo('sqlsrv:server=' . $options['server'] . ';database=' . $options['database'], $options['user'], $options['password']); $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); $pdo->query('set character_set_connection=' . $options['charset'] . ', character_set_results=' . $options['charset'] . ', character_set_client=' . $options['charset']); $result = $pdo->query($sql); if ($result) { $row = $result->fetch(); return $row ? implode(',', $row) : ''; } } catch (Exception $e) { return $e; } } else { return '系统错误: 连接串不正确,请检查是否包含server, user, password, database, charset参数。'; } return ''; }