如何在另一个页面中调用php pdo连接类

时间:2015-01-20 13:59:21

标签: php pdo

我在db.php类中创建了一个连接,如下所示:

class db
    {   
        public $isConnected;
        protected $database;

        public function __construct($username, $password, $host, $dbname, $options=array())
        {
            $this->isConnected = true;
            try { 
                $this->database= new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options); 
                $this->database->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
                $this->database->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
            } 
            catch(PDOException $e) { 
                $this->isConnected = false;
                throw new Exception($e->getMessage());
            }
        }       

public function select($query, $params=array()){
            try{ 
                $stmt = $this->database->prepare($query); 
                $stmt->execute($params);
                return $stmt->fetch();  
                }catch(PDOException $e){
                throw new Exception($e->getMessage());
            }
        }

    }

我在state.php页面中访问此类,如下所示:

<?php
$database = new db("root", "", "localhost", "bhaskar_hindi_dbs", array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));  
                $getrows = $database->select("SELECT state_id, state_name FROM state");         
                foreach($getrows as $row){                  
                ?>

但我的项目中有多个页面。现在我需要在db类中设置下面这个变量,并根据这个建立一个连接。

$host = localhost;

$username = "";

$password ="";

$dbname = "";

$options = "";

任何人都可以建议如何在DB类中设置此变量以及如何从state.php页面调用DB类?

4 个答案:

答案 0 :(得分:0)

将此添加到state.php文件的顶部

require 'db.php';

这个如果db.php文件在同一目录下,如果不是那么

require 'path_to_where_the_db.php_file_is/db.php';

答案 1 :(得分:0)

如果你在state.php中传递它们我没有看到问题,但是如果你不想要你可以这样做:

class db
    {   
        public $isConnected;
        protected $database;
        private $_dbOptionArray;
        const DB_USER = 'root';
        const DB_PASS = 'pass';
        const DB_HOST = 'host';
        const DB_NAME = 'name';




        public function __construct($username, $password, $host, $dbname, $options=array())
        {
         $this->_dbOptionArray = [];//set your options here
            $this->isConnected = true;
            try { 
                $this->database= new PDO("mysql:host={self::DB_HOST};dbname={self::DB_NAME};charset=utf8", self::DB_USER, self::DB_PASS, $this->_dbOptionArray); 
                $this->database->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
                $this->database->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
            } 
            catch(PDOException $e) { 
                $this->isConnected = false;
                throw new Exception($e->getMessage());
            }
        }       

public function select($query, $params=array()){
            try{ 
                $stmt = $this->database->prepare($query); 
                $stmt->execute($params);
                return $stmt->fetch();  
                }catch(PDOException $e){
                throw new Exception($e->getMessage());
            }
        }

    }

答案 2 :(得分:0)

通过创建工厂来设置参数。

<强> DatabaseFactory.php

include("db.php");

class DatabaseFactory
{
    protected $db;

    public function __construct()
    {
        $this->db = new db("root", "", "localhost", "bhaskar_hindi_dbs", array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
    }

    public function getConnection()
    {
        return $this->db;
    }
}

然后,您可以在其他页面中包含DatabaseFactory.php,而无需每次都重新输入参数。然后像这样使用它:

include "DatabaseFactory.php";
$DBFactory = new DatabaseFactory();
$Database = $DBFactory->getConnection();
$query = ...; // your query
$Database->select($query);

如果您愿意,您可以轻松更改参数,在所有页面上重复使用或以后的方式更改数据库类型。

答案 3 :(得分:-1)

您可以使用其他功能扩展父功能。

class dbConfig extends db {

    private $engine;
    private $host;
    private $database;
    private $user;
    private $pass;

    public function __construct(){
        $this->engine = 'mysql';
        $this->host = 'localhost';
        $this->database = '';
        $this->user = '';
        $this->pass = '';
        $dns = $this->engine.':dbname='.$this->database.";host=".$this->host;
        parent::__construct( $dns, $this->user, $this->pass );
    }
}