2012-08-30 64 views

回答

1

只要打開2個數據庫連接,一個用於mysql_*函數,一個用於PDO,這應該沒有任何問題。

唯一的潛在缺點是兩個數據庫連接而不是一個臨時的額外開銷。

+0

這是我的下一個問題。我可以在同一個connect.php文件中創建新的連接,還是需要使用PDO連接創建一個附加的connect.php文件? – Colbyd

+0

@Colbyd你創建連接的地方並不重要,只要它是一個PDO連接即可。 – jeroen

+1

@Jeroen ......感謝您的迴應。 – Colbyd

1

我不明白它爲什麼會這樣,除非你使用某種特殊的數據庫處理程序類或某種東西。

+0

我是否需要創建一個新的connect.php文件,該文件具有PDO連接,或者可以在所有功能更改後再做此操作? – Colbyd

+0

@ jeroen說你需要一個新的連接。 – Tynarus

+0

@Tynarus ....感謝您的快速回復 – Colbyd

1

您可能要考慮的一件事是不使用「連接」腳本,而是使用更多的OOP /數據模型設置。

基本上,您將您的連接細節保存在一個單獨的文件中 - 我只是定義了一些常量,稍後我們可以在包含它的腳本中訪問這些常量。從那裏,你創建一個類,在實例化時負責建立它自己的連接。這個類將包含對應於你的典型查詢的方法,也許有一種方法可以根據需要運行原始查詢。

這樣做的好處是,您可以基本上保留現有代碼,並在您更新或替換現有腳本時,在需要的位置添加新的數據模型代碼。

作爲參考的緣故,這裏的代碼精簡版我用:

db.php中

<?php 
    # Set the database access information as constants. 
    DEFINE ('DB_USER', 'your_db_user_name'); 
    DEFINE ('DB_PASSWORD', 'your-super-duper-secret-password'); 
    DEFINE ('DB_HOST', 'localhost'); 
    DEFINE ('DB_NAME', 'schema-name'); 
    DEFINE ('DB_CONNECTION', 'mysql:host=' . DB_HOST . ';dbname=' . DB_NAME); 
?> 

博客,model.php

<?php 
    # File:  blog-model.php 
    # Version: 1.0 
    # Updated: 2011.9.4 
    # Meta:  This file contains the database access information. 
    #    This file also establishes a connection to MySQL and selects the database. 

    @require_once(ROOT . DS . 'config' . DS . 'db.php'); 

    # Utility Class 
    class BlogModel { 

     protected $pdo; 

     # Constructor 
     function __construct() { 
      $this->connect(); 
     } 

     function __destruct() { 
     } 

     # Connect to the database 
     function connect() { 
      # Database connectivity can be a tricky beast, so I'm wrapping the entire block in a try/catch 
      try { 
       $this->pdo = new PDO(DB_CONNECTION, DB_USER, DB_PASSWORD, array(PDO::ATTR_PERSISTENT => true)); 

       # Set character set to UTF-8 (adds support for non-ASCII languages). 
       # Note this can cause issues with BLOB-style fields, especially with INSERTs 
       $this->pdo->exec("SET CHARACTER SET utf8"); 
       return true; 
      } 
      catch(PDOException $e) { 
       # Add code to write out error log and alert administrator 
       trigger_error("<p>Could not select the database</p>\n<p>MySQL Error: " . $e->getMessage() . "</p>"); 
       return false; 
      } 
     } 

     # Run an INSERT query; that is, insert a new row (or rows) into a MySQL table 
     function insert($authorid, $title, $permalink, $category, $body, $tags, $abstract) { 
      try { 
       # Named parameters (prefered) 
       $stmt = $this->pdo->prepare( 
        "INSERT INTO pages 
        SET title = :title, 
        permalink = :permalink, 
        category = :category, 
        body = :body, 
        tags = :tags, 
        abstract = :abstract, 
        author = :authorid, 
        timestamp = NOW();" 
        ); 

       $stmt->bindParam(':title', $title); 
       $stmt->bindParam(':permalink', $permalink); 
       $stmt->bindParam(':category', $category); 
       $stmt->bindParam(':body', $body); 
       $stmt->bindParam(':tags', $tags); 
       $stmt->bindParam(':abstract', $abstract); 
       $stmt->bindParam(':authorid', $authorid, PDO::PARAM_INT); 

       return $stmt->execute(); 
      } 
      catch(Exception $e) { 
       # Add code to write out error log and email administrator 
       trigger_error("<p>An error occurred whilst executing your query:\n<br />MySQL Error: " . $e->getMessage() . "</p>"); 
       return false; 
      } 
     } 

     # Run an UPDATE query; that is, update an existing row (or rows) in a MySQL table 
     function update($id, $title, $category, $body, $tags, $abstract) { 
      try { 
       # Update the project matching the supplied id 
       # Named parameters (prefered) 
       $stmt = $this->pdo->prepare( 
        "UPDATE pages 
        SET title = :title, category = :category, body = :body, tags = :tags, abstract = :abstract, lastupdated = NOW() 
        WHERE permalink = :id 
        LIMIT 1;" 
        ); 

       $stmt->bindParam(':id', $id); 
       $stmt->bindParam(':title', $title); 
       $stmt->bindParam(':category', $category); 
       $stmt->bindParam(':body', $body); 
       $stmt->bindParam(':tags', $tags); 
       $stmt->bindParam(':abstract', $abstract); 

       return $stmt->execute(); 
      } 
      catch(Exception $e) { 
       # Add code to write out error log and email administrator 
       trigger_error("<p>An error occurred whilst executing your query:\n<br />MySQL Error: " . $e->getMessage() . "</p>"); 
       return false; 
      } 

     } 

     # Run a DELETE query; that is, remove a record (or records) from a MySQL table 
     function delete($id) { 
      try { 
       # Delete the project matching the supplied id 
       # Named parameters (prefered) 
       $stmt = $this->pdo->prepare("DELETE FROM pages WHERE id = :id LIMIT 1;"); 

       $stmt->bindParam(':id', $id, PDO::PARAM_INT); 

       return $stmt->execute(); 
      } 
      catch(Exception $e) { 
       # Add code to write out error log and email administrator 
       trigger_error("<p>An error occurred whilst executing your query:\n<br />MySQL Error: " . $e->getMessage() . "</p>"); 
       return false; 
      } 
     } 

     # Close the connection 
     function close() { 
      $this->pdo = null; 
     } 
    } 
?> 

這是所有這些都可能與您的原始問題不完全相關,但也許您(或某些隨機的Google-er)可以從中獲得一些用處。

+0

感謝輸入我肯定會堅持這一點,當我開始這個過程。 – Colbyd

相關問題