本文整理汇总了PHP中PDO类的典型用法代码示例。如果您正苦于以下问题:PHP PDO类的具体用法?PHP PDO怎么用?PHP PDO使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了PDO类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: prepare
/**
* Returns a PDOStatement
*
* This method will return the same object for equal queries.
*
* @param string $sql
* @return \PDOStatement
* @throws \PDOException
*/
public function prepare($sql)
{
if (!array_key_exists($sql, $this->statements)) {
$this->statements[$sql] = $this->pdo->prepare($sql);
}
return $this->statements[$sql];
}
示例2: testEquipmentUpdate
public function testEquipmentUpdate()
{
$this->delete($this->insert(array(Object::TIME_IN_SECONDS => 3600, Object::DISTANCE => 12, Object::SHOEID => 1)));
$this->delete($this->insert(array(Object::TIME_IN_SECONDS => 3600, Object::DISTANCE => 12, Object::SHOEID => 2)));
$this->assertEquals(array('km' => 10, 'time' => 3000), $this->PDO->query('SELECT `km`, `time` FROM `' . PREFIX . 'shoe` WHERE `id`=1 AND `accountid`=0')->fetch(PDO::FETCH_ASSOC));
$this->assertEquals(array('km' => 0, 'time' => 0), $this->PDO->query('SELECT `km`, `time` FROM `' . PREFIX . 'shoe` WHERE `id`=2 AND `accountid`=0')->fetch(PDO::FETCH_ASSOC));
}
示例3: setUp
public function setUp(PDO $pdo, $sql)
{
$sql = explode(';', trim($sql));
foreach ($sql as $query) {
$pdo->exec(trim($query));
}
}
示例4: getMysqlConnexion
public static function getMysqlConnexion()
{
$db = new \PDO('mysql:host=192.168.220.11;dbname=news', 'root', 'greg');
# $db = new \PDO('mysql:host=192.168.2.1;dbname=news', 'root', 'root');
$db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
return $db;
}
示例5: updateImage
public static function updateImage($id, $img)
{
$sql = "UPDATE yy_article SET img = '{$img}' WHERE id = {$id}";
$config = self::getDb()->getConfig();
$pdo = new PDO("mysql:host={$config['host']};dbname={$config['dbname']}", $config['username'], $config['password']);
return $pdo->exec($sql);
}
示例6: 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;
}
示例7: wp_theme_display_theme
/**
* To use this theme, you need to set up a database "wp_example" with a table called "wp_posts" (Whata Primitive Posts)
* and give it a column 'post_title'
*/
function wp_theme_display_theme()
{
$hostname = 'localhost';
$user = 'username';
$pass = 'password';
$database = 'wp_example';
$db_connection = new PDO("mysql:host=" . $hostname . ";dbname=" . $database, $user, $pass);
$results = $db_connection->query('SELECT post_title FROM wp_posts WHERE 1 ORDER by rand() LIMIT 1');
$post_title = $results[0]['post_title'];
?>
<!doctype html>
<html>
<head>
<title>WP CMS</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1><?php
echo $post_title;
?>
</h1>
</body>
</html>
<?php
}
示例8: setUp
public function setUp()
{
date_default_timezone_set('UTC');
if (DB_DRIVER === 'mysql') {
$pdo = new PDO('mysql:host=' . DB_HOSTNAME, DB_USERNAME, DB_PASSWORD);
$pdo->exec('DROP DATABASE ' . DB_NAME);
$pdo->exec('CREATE DATABASE ' . DB_NAME);
$pdo = null;
} elseif (DB_DRIVER === 'postgres') {
$pdo = new PDO('pgsql:host=' . DB_HOSTNAME, DB_USERNAME, DB_PASSWORD);
$pdo->exec('DROP DATABASE ' . DB_NAME);
$pdo->exec('CREATE DATABASE ' . DB_NAME . ' WITH OWNER ' . DB_USERNAME);
$pdo = null;
}
$this->container = new Pimple\Container();
$this->container->register(new Kanboard\ServiceProvider\DatabaseProvider());
$this->container->register(new Kanboard\ServiceProvider\ClassProvider());
$this->container['dispatcher'] = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
$this->container['db']->logQueries = true;
$this->container['logger'] = new Logger();
$this->container['logger']->setLogger(new File('/dev/null'));
$this->container['httpClient'] = new FakeHttpClient();
$this->container['emailClient'] = $this->getMockBuilder('EmailClient')->setMethods(array('send'))->getMock();
$this->container['userNotificationType'] = $this->getMockBuilder('\\Kanboard\\Model\\UserNotificationType')->setConstructorArgs(array($this->container))->setMethods(array('getType'))->getMock();
}
示例9: cs_sql_connect
function cs_sql_connect($cs_db, $test = 0)
{
$error = '';
if (!extension_loaded('pdo') or !extension_loaded('pdo_mysql')) {
$error = 'PHP extensions pdo and pdo_mysql must be activated!';
} else {
$param = empty($cs_db['place']) ? '' : 'host=' . $cs_db['place'] . ';';
$param .= 'dbname=' . $cs_db['name'];
try {
$connect = new PDO('mysql:' . $param, $cs_db['user'], $cs_db['pwd']);
} catch (PDOException $err) {
$error = $err->getMessage();
}
}
global $cs_main;
$sql_charset = strtolower($cs_main['charset']);
if (empty($error) and $sql_charset == 'utf-8') {
$connect->exec("SET NAMES 'utf8'");
}
if (empty($test) and empty($error)) {
return $connect;
} elseif (empty($test)) {
cs_error_sql(__FILE__, 'cs_sql_connect', $error, 1);
} else {
return $error;
}
}
示例10: __construct
public function __construct()
{
//J'initialise quelques variables.
if ($this->table === false) {
$this->table = strtolower(get_class($this)) . 's';
}
// Je me connecte à la base.
$conf = Conf::$database[$this->conf];
if (isset(Model::$connection[$this->conf])) {
$this->db = Model::$connection[$this->conf];
return true;
}
try {
$pdo = new PDO('mysql:host=' . $conf['host'] . ';dbname=' . $conf['database'] . ';', $conf['login'], $conf['password'], array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
Model::$connection[$this->conf] = $pdo;
$this->db = $pdo;
} catch (PDOException $e) {
if (Conf::$debug >= 1) {
die($e->getMessage());
} else {
die('Impossible de se connecter à la base de donnée.');
}
}
}
示例11: getDatabase
function getDatabase()
{
$config = array('DB_DNS' => 'mysql:host=localhost;port=3306;dbname=phpclasssummer2015', 'DB_USER' => 'root', 'DB_PASSWORD' => '');
$db = new PDO($config['DB_DNS'], $config['DB_USER'], $config['DB_PASSWORD']);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
return $db;
}
示例12: getdb_content
/**
* 连接数据库
*
*/
private function getdb_content()
{
$db = new PDO($this->dsn_content, $this->dbuser_content, $this->dbpasswd_content);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->exec('set names utf8');
return $db;
}
示例13: connectDB
public function connectDB()
{
try {
$db_host = 'localhost';
// hostname
$db_name = 'matheno_mygig';
// databasename
$db_user = 'matheno_marijke';
// username
$user_pw = 'Gabruko3';
// password
$con = new PDO('mysql:host=' . $db_host . '; dbname=' . $db_name, $db_user, $user_pw);
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$con->exec("SET CHARACTER SET utf8");
// return all sql requests as UTF-8
return $con;
} catch (PDOException $err) {
echo "harmless error message if the connection fails";
$err->getMessage() . "<br/>";
file_put_contents('PDOErrors.txt', $err, FILE_APPEND);
// write some details to an error-log outside public_html
die;
// terminate connection
}
}
示例14: authenticate
function authenticate($username, $password) {
$db = new PDO('mysql:dbname=dwa;host=localhost;charset=utf8', 'dbuser', '123');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
$stmt = $db->prepare("SELECT zaporka FROM korisnik WHERE korIme = :username");
$stmt->bindParam(':username', $username);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$hash = $result['zaporka'];
if(!password_verify($password, $hash)) {
return false;
}
$stmt = $db->prepare("SELECT id, korIme, ime FROM korisnik WHERE korIme = :username");
$stmt->bindParam(':username', $username);
$stmt->execute();
$user = $stmt->fetch();
$_SESSION['user'] = $user['korIme'];
return true;
} catch(PDOException $ex) {
echo "Nes ne valja: ".$ex->getMessage();
return false;
}
}
示例15: getUserData
function getUserData($username)
{
try {
$userarray = array();
#echo "username: $username<br/>";
$db = new PDO('mysql:dbname=michaeq6_mheartsj', 'michaeq6_mj', 'mj060708');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $db->prepare("SELECT group_id FROM users WHERE name=?");
$stmt->bindParam(1, $username);
$completed = $stmt->execute();
$array = $stmt->fetch(PDO::FETCH_BOTH);
#echo "group id: " . $array['group_id'] . "<br/>";
$userarray['gid'] = $array['group_id'];
$stmt = $db->prepare("SELECT password FROM groups WHERE id=?");
$stmt->bindParam(1, $array['group_id']);
$completed = $stmt->execute();
$array = $stmt->fetch(PDO::FETCH_BOTH);
#echo "password: " . $array['password'] . "<br/>";
$userarray['password'] = $array['password'];
#return $array['password'];
return $userarray;
} catch (PDOException $e) {
echo $e->getMessage();
}
return false;
}