使用PDO从Mssql获取数据到Mysql数据库

时间:2013-08-26 20:02:14

标签: mysql sql-server database pdo

我从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();
    }

1 个答案:

答案 0 :(得分:1)

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