本文整理汇总了PHP中PDO::setAttribute方法的典型用法代码示例。如果您正苦于以下问题:PHP PDO::setAttribute方法的具体用法?PHP PDO::setAttribute怎么用?PHP PDO::setAttribute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PDO
的用法示例。
在下文中一共展示了PDO::setAttribute方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: createPDO
function createPDO()
{
$pdo = new \PDO('mysql:host=' . BOT_DB_HOST . ';dbname=' . BOT_DB_NAME . ';charset=utf8', BOT_DB_USER, BOT_DB_PASSWORD);
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
return $pdo;
}
示例2: getConnection
/**
* 根据读写性要求设置一个适合的pdo对象
*
* @param $rw 服务器的读写性
* @return object
*/
public function getConnection($rw = 'rw')
{
$params = $this->_params;
//如果是只读查询,从所有服务器中随机挑选一台
if ($rw == 'r') {
$rw = 'all';
}
if (isset($this->_pdos[$rw]) && $this->_pdos[$rw] instanceof PDO) {
$this->_pdo = $this->_pdos[$rw];
return $this->_pdo;
}
//随机从满足读写性要求的服务器中挑选出一台服务器
$index = rand(0, sizeof($params[$rw]) - 1);
$params = $params[$rw][$index];
if (!isset($params['port'])) {
$params['port'] = '3306';
}
$dsn = "mysql:host={$params['host']};port={$params['port']};dbname={$params['dbname']}";
$pdo = new PDO($dsn, $params['username'], $params['password']);
if ($params['persistent']) {
$pdo->setAttribute(PDO::ATTR_PERSISTENT, true);
}
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec("SET NAMES {$params['charset']}");
$this->_pdo = $pdo;
$this->_pdos[$rw] = $pdo;
return $this->_pdo;
}
示例3: connect
/**
* @throws DBException
* @return Resource
*/
public function connect()
{
if ($this->link != null) {
return true;
}
$_config = $this->config;
$_dsn = "{$_config['db_type']}:host={$_config['db_host']};port={$_config['db_port']};dbname={$_config['db_name']}";
try {
$this->link = new PDO($_dsn, $_config['db_user'], $_config['db_pass'], array(PDO::ATTR_PERSISTENT => false));
$this->link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//设置数据库编码,默认使用UTF8编码
$_charset = $_config['db_charset'];
if (!$_charset) {
$_charset = 'UTF8';
}
$this->link->query("SET names {$_charset}");
$this->link->query("SET character_set_client = {$_charset}");
$this->link->query("SET character_set_results = {$_charset}");
} catch (PDOException $e) {
if (APP_DEBUG) {
E("数据库连接失败" . $e->getMessage());
}
}
return $this->link;
}
示例4: setDefaultAttributes
/**
* Setting default values on the PDO instance like errormode, encoding and
* so on.
*/
public function setDefaultAttributes()
{
$this->_pdo->setAttribute(\PDO::ATTR_CASE, \PDO::CASE_NATURAL);
$this->_pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$this->_pdo->setAttribute(\PDO::ATTR_ORACLE_NULLS, \PDO::NULL_TO_STRING);
$this->_pdo->query("SET NAMES utf8");
}
示例5: getConnection
function getConnection()
{
switch ($_SERVER['SERVER_NAME']) {
case DEVELOPMENT:
// development server
// header('Access-Control-Allow-Origin: http://localhost:9000');
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "test";
break;
default:
// live server
// header('Access-Control-Allow-Origin: http://beatle.par-ken.com');
$dbhost = "sql.ayyayo.com";
$dbuser = "parken";
$dbpass = "pankaj5666";
$dbname = "parken_healthylife";
break;
}
$dbh = new PDO("mysql:host={$dbhost};dbname={$dbname}", $dbuser, $dbpass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
return $dbh;
}
示例6: set_connection
/**
* @param \PDO $conn
* @return bool if connection if actually connected
*/
public function set_connection(\PDO $conn)
{
$this->_connection = $conn;
//Throw exeption when INSERT fail (Logs should made the whole app fail. Lol.)
$this->_connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
return $this->_is_connected();
}
示例7: dbconnect
function dbconnect()
{
$db = new PDO('mysql:host=localhost;dbname=makfood;charset=utf8', 'root', '');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
return $db;
}
示例8: init
protected function init()
{
if (!$this->is_initialised) {
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->is_initialised = TRUE;
}
}
示例9: __construct
/**
* @param String $prefix the prefix of the table names.
*/
public function __construct(\PDO $pdo, $prefix = "bav_")
{
$this->pdo = $pdo;
$this->prefix = $prefix;
$this->statementContainer = new StatementContainer($pdo);
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
}
示例10: __construct
public function __construct($path)
{
if ($path !== ':memory:' && !is_file($path)) {
touch($path);
// ensures ordinary file permissions
}
$this->pdo = new \PDO('sqlite:' . $path);
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$this->pdo->exec('
PRAGMA foreign_keys = ON;
CREATE TABLE IF NOT EXISTS cache (
key BLOB NOT NULL PRIMARY KEY,
data BLOB NOT NULL,
expire INTEGER,
slide INTEGER
);
CREATE TABLE IF NOT EXISTS tags (
key BLOB NOT NULL REFERENCES cache ON DELETE CASCADE,
tag BLOB NOT NULL
);
CREATE INDEX IF NOT EXISTS cache_expire ON cache(expire);
CREATE INDEX IF NOT EXISTS tags_key ON tags(key);
CREATE INDEX IF NOT EXISTS tags_tag ON tags(tag);
PRAGMA synchronous = OFF;
');
}
示例11: login
function login()
{
$db = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'root', 'root');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
if (!empty($_POST['contact_submitted'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$stmt = $db->prepare("SELECT * FROM users WHERE username=?");
$stmt->execute(array($username));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (!$rows) {
echo "<script>alert('wrong username/password');</script>";
}
/*
Than we validate the password, if the validation is true than we set the sessions
For more detailed information on password validation check please look into the
Password storage(salting/stretching/hashing) in the knowledgebase for more information.
*/
foreach ($rows as $loginUser) {
if ($loginUser['password'] == $password) {
session_start();
$_SESSION['userID'] = $loginUser['userID'];
$_SESSION['access'] = "active";
//The CSRF token is set here by an aproved random number generator
$_SESSION['csrf'] = base64_encode(openssl_random_pseudo_bytes(128));
header("location:loggedin.php?userID=" . $loginUser['userID'] . "");
return $loginUser;
} else {
echo "<script>alert('wrong password!');</script>";
}
}
}
}
示例12: connect_db
/**
* Connect to a database via FS_PDO
*
* @param mixed $dsn data source for database connection (array or string)
* @param $login
* @param $password
*/
public function connect_db($dsn, $login, $password)
{
try {
$this->db = new PDO($dsn, $login, $password);
} catch (PDOException $e) {
$this->debug($e->getMessage());
$this->file_not_found();
//program terminates in function file_not_found()
}
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$driver = $this->db->getAttribute(PDO::ATTR_DRIVER_NAME);
$this->debug("our driver is {$driver}");
switch ($driver) {
case 'mysql':
$quoter = '`';
break;
case 'pgsql':
if (DEFAULT_DSN_SCHEMA) {
$this->db->exec('SET SEARCH_PATH=' . DEFAULT_DSN_SCHEMA);
}
$quoter = '"';
break;
default:
$quoter = '';
break;
}
define('DB_FIELD_QUOTE', $quoter);
}
示例13: db_mysql_init
function db_mysql_init(PDO $pdo)
{
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_EMPTY_STRING);
$pdo->setAttribute(PDO::ATTR_AUTOCOMMIT, false);
$pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
}
示例14: setUp
protected function setUp()
{
$this->PDO = DB::getInstance();
$this->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->PDO->exec('INSERT INTO `runalyze_training` (`accountid`) VALUES (0)');
$this->ActivityID = $this->PDO->lastInsertId();
}
示例15: setUp
protected function setUp()
{
$this->PDO = new PDO('sqlite::memory:');
$this->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->PDO->exec('CREATE TEMP TABLE `' . PREFIX . 'withoutid` ( `foo` VARCHAR(10), `arr` VARCHAR(100) )');
$this->PDO->exec('CREATE TEMP TABLE `' . PREFIX . 'withid` ( `id` INTEGER PRIMARY KEY AUTOINCREMENT, `foo` VARCHAR(10) )');
}