public static function setUpBeforeClass() { $dsn = sprintf("sqlite:%s", '././test_useres.db'); self::$pdo = new \PDO($dsn); self::$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); $sth = self::$pdo->prepare("\n CREATE TABLE users (\n id INTEGER PRIMARY KEY AUTOINCREMENT,\n username VARCHAR(128) NOT NULL UNIQUE,\n password_hash VARCHAR(64) NOT NULL);"); $sth->execute(); }
/** * Constructs the database cache * Be aware that the table must exist before you can cache! * * @param string $dsn the DSN needed for PDO to connect to the database * @param string $username the username for the database connection * @param string $password the password for the database connection * @param bool|false|string $table the table name for the cache (defaults to "phealng-cache") * @param array $dbOptions (optional) additional options for PDO * @throws PhealException */ public function __construct($dsn, $username, $password, $table = false, array $dbOptions = array()) { $this->db = new \PDO($dsn, $username, $password, $dbOptions); $this->db->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC); $this->db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); if ($table !== false) { if (strpos($table, '`') !== false) { throw new PhealException('The table name mustn\'t have backticks!'); } $this->options['table'] = $table; } }
function connect() { try { $connexion = new Pdo('mysql: host=' . HOST . ';dbname=' . DBNAME, USER, PW); $connexion->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); return $connexion; } catch (PDOexception $e) { echo "problème de connexion" . $e->getMessage(); return false; } }
/** * 这个方法就别覆盖了~ * 也别自己造别的方法了,主要是害怕忘了设置PDO::ATTR_ERRMODE * * @param PDO $pdo */ public final function initPdo(PDO $pdo) { $this->pdo = $pdo; //设置错误处理方式,通过返回值来表达。既不要去log warning,也不要throw exception $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT); }
$conferenceId = 3664; /** * Default speaker URI * * Change this value to specify a different default speaker URI when a speaker does not have an entry on joind.in. */ $defaultSpeakerUri = 'http://www.zendcon.com'; /** * Default speaker twitter username * * Change this value to specify a different default speaker twitter username when a speaker does not have an entry on joind.in. */ $defaultSpeakerTwitter = 'zendcon'; $dbPath = realpath(__DIR__ . '/../data/db/conference.db'); $pdo = new Pdo('sqlite:' . $dbPath); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $insertSpeaker = $pdo->prepare('INSERT INTO speakers (id, name, url, twitter) VALUES (:id, :name, :url, :twitter)'); $insertTalk = $pdo->prepare('INSERT INTO talks (id, title, abstract, day, start_time) VALUES (:id, :title, :abstract, :day, :start_time)'); $insertLink = $pdo->prepare('INSERT INTO talks_speakers (talk_id, speaker_id) VALUES (:talk_id, :speaker_id)'); $client = new Client(); $client->setOptions(['adapter' => Curl::class]); $request = $client->getRequest(); $headers = $request->getHeaders(); $headers->addHeaderLine('Accept', 'application/json'); $client->setUri(sprintf('http://api.joind.in/v2.1/events/%d/talks?verbose=yes&resultsperpage=0', $conferenceId)); $client->setMethod('GET'); printf("Fetching talks for ZendCon 2015..."); $response = $client->send(); $json = $response->getBody(); $payload = json_decode($json); $talks = $payload->talks;
<?php require_once __DIR__ . '/../vendor/autoload.php'; require_once __DIR__ . '/../lib/highlight.php'; /** * We don't need no stinking architecture!1!! * * 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
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 ''; }