1

PDO に変換する必要がある mysql クエリがたくさんあります。一度に 1 つのクエリを実行して、他のすべての機能を引き続き使用することはできますか? たとえば、1 つのクエリを PDO に変換すると、他のすべての mysql クエリが正しく機能しなくなりますか?

4

3 に答える 3

1

ある種の特別なデータベース ハンドラ クラスなどを使用していない限り、なぜそうなるのかわかりません。

于 2012-08-30T01:37:41.367 に答える
1

mysql_*関数用と PDO用の 2 つのデータベース接続が開いている限り、これは問題なく動作するはずです。

唯一の潜在的な欠点は、1 つではなく 2 つの db 接続の一時的な余分なオーバーヘッドです。

于 2012-08-30T01:37:50.700 に答える
1

考慮すべきことの 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 );
?>

ブログモデル.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)がそれから何らかの用途を引き出すことができます.

于 2012-08-30T02:20:24.843 に答える