當前位置: 首頁>>代碼示例>>PHP>>正文


PHP PDO類代碼示例

本文整理匯總了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];
 }
開發者ID:bmdevel,項目名稱:bav,代碼行數:16,代碼來源:StatementContainer.php

示例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));
 }
開發者ID:schoch,項目名稱:Runalyze,代碼行數:7,代碼來源:DeleterTest.php

示例3: setUp

 public function setUp(PDO $pdo, $sql)
 {
     $sql = explode(';', trim($sql));
     foreach ($sql as $query) {
         $pdo->exec(trim($query));
     }
 }
開發者ID:krajewskis,項目名稱:ppdo,代碼行數:7,代碼來源:AbstractQueryTest.php

示例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;
 }
開發者ID:gbecchio,項目名稱:tp_poo_cache,代碼行數:7,代碼來源:PDOFactory.php

示例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);
 }
開發者ID:baiooy,項目名稱:hqimt_backend,代碼行數:7,代碼來源:Article.php

示例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;
 }
開發者ID:iamlaobie,項目名稱:hi-php-framework,代碼行數:34,代碼來源:Mysql.php

示例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 
}
開發者ID:andyadams,項目名稱:whata-primitive-theme,代碼行數:30,代碼來源:functions.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();
 }
開發者ID:namaljayathunga,項目名稱:kanboard,代碼行數:25,代碼來源:Base.php

示例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;
    }
}
開發者ID:aberrios,項目名稱:WEBTHESGO,代碼行數:27,代碼來源:pdo_mysql.php

示例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.');
         }
     }
 }
開發者ID:EmmanuelMarti,項目名稱:Koffee4Free,代碼行數:25,代碼來源:Model.php

示例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;
}
開發者ID:AbigailRDyer,項目名稱:PHPClassSummer2015_Home,代碼行數:7,代碼來源:dbConn.php

示例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;
 }
開發者ID:haitao1880,項目名稱:study,代碼行數:11,代碼來源:Wifi_Ap.php

示例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
     }
 }
開發者ID:MHakvoort,項目名稱:origin,代碼行數:25,代碼來源:Config.php

示例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;
            }
    }
開發者ID:Tropuska,項目名稱:labosi,代碼行數:33,代碼來源:auth.php

示例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;
}
開發者ID:abc2mit,項目名稱:abc2mit.github.io,代碼行數:26,代碼來源:rsvp.php


注:本文中的PDO類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。