-1

我正在从 Windows 平台中的会计程序获取数据到我的 PHP 脚本。我正在将数据从 mssql 传输到 mysql。我决定使用 PDO。我有过渡的问题。我怎样才能同时提供两个连接?

这是我自己的 PDO 类。

class Database {
    protected $_host           = "***********";
    protected $_engine         = "mysql";
    protected $_dbuser         = "***********";
    protected $_dbpassword     = "***********";
    protected $_db             = "***********";

    protected $_sql;
    protected $pdo;




    /* @desc This Function is Setting SQL Connection
     * @return SQL Connection
     *
     */
    protected function getPdo()
    {
        if ($this->pdo === NULL) {
            try {

              $dsn = $this->_engine.':dbname='.$this->_db.';host='.$this->_host.';charset=utf8';

              $this->pdo = new PDO($dsn, $this->_dbuser, $this->_dbpassword, array(
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"
              ));
              //$this->pdo->setAttribute();
              //$this->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'");

            } catch (PDOException $e) {
              echo 'Connection failed: ' . $e->getMessage();
            }
        }
        return $this->pdo;
    }




    /* @desc This Function is Preparing Sql Statement For Query
     * @return array Returns PDO Object Query
     *
     */
    public function query($sql)
    {
        return $this->_sql = $this->getPdo()->prepare($sql);
    }









    /* @desc This Function is for Binding Values into SQL Statements
     * @return Binded SQL Statements
     *
     */
    public function bind($param, $value, $type = null){
        if (is_null($type)) {
          switch (true) {
            case is_int($value):
              $type = PDO::PARAM_INT;
              break;
            case is_bool($value):
              $type = PDO::PARAM_BOOL;
              break;
            case is_null($value):
              $type = PDO::PARAM_NULL;
              break;
            default:
              $type = PDO::PARAM_STR;
          }
        }
        $this->_sql->bindValue($param, $value, $type);
    }








    /* @desc This Function Execute Query
     * @return SQL Execution
     *
     */
    public function execute($values = NULL){
        return (!isset($values)) ? $this->_sql->execute() : $this->_sql->execute($values);
    }








    /* @desc This Function Execute Query and Fetch Multiple Result
     * @return array Returns Array of Table Rows. Array is multidimensional
     *
     */
    public function queryResults(){
      $this->execute();
      return $this->_sql->fetchAll(PDO::FETCH_ASSOC);
    }






    /* @desc This Function Execute Query and Fetch Only Single Result
     * @return array Returns Array of Table Row. Array is not multidimensional.
     *
     */
    public function queryResult(){
        $this->execute();
        return $this->_sql->fetch(PDO::FETCH_ASSOC);
    }




    public function queryColumn($index = NULL){
        $index = (isset($index)) ? intval($index) : 0;
        $this->execute();
        return $this->_sql->fetchAll(PDO::FETCH_COLUMN,$index);
//        $this->execute();
//        return $this->_sql->fetchAll(PDO::FETCH_COLUMN);
    }







    /* @desc This Function Returns Effected Row Count during Update, Delete and Insert
     * @return int Returns Effected Row Count
     *
     */
    public function effected(){
      return $this->_sql->rowCount();
    }






    /* @desc This Function Returns Effected Row Count during Update, Delete and Insert
     * @return int Returns Effected Row Count
     *
     */
    public function rowCount(){
      return count($this->queryResults());
    }


    public function lastID(){
        return $this->_sql->lastInsertId();
    }
4

1 回答 1

1

您需要创建 2 个 PDO 类实例 - 一个用于 MSSQL,一个用于 MySQL。

于 2013-08-27T04:35:42.840 回答