1

我有一堆 mysql 查询,我需要转换为 PDO。我可以一次执行一个查询,而所有其他功能都可以继续工作吗?例如,如果我将 1 个查询转换为 PDO,这会阻碍我所有其他 mysql 查询的正常工作?

4

3 回答 3

1

我不明白为什么会这样,除非您使用某种特殊的数据库处理程序类或其他东西。

于 2012-08-30T01:37:41.367 回答
1

只要您打开了 2 个数据库连接,一个用于mysql_*函数,一个用于 PDO,这应该可以正常工作。

唯一的潜在缺点是两个数据库连接而不是一个的临时额外开销。

于 2012-08-30T01:37:50.700 回答
1

您可能要考虑的一件事是不使用“连接”脚本,而是使用更多的 OOP/数据模型设置。

基本上,您将连接详细信息保存在一个单独的文件中 - 我的只是定义了一些常量,稍后我可以在包含它的脚本中访问这些常量。从那里,您创建一个类,该类负责在实例化时建立它自己的连接。此类将包含与您的典型查询相对应的方法,可能还有一个根据需要运行原始查询的方法。

这样做的好处是您基本上可以不理会现有代码,只需在需要的位置添加新的数据模型代码,即可更新或替换现有脚本。

作为参考,这里是我曾经使用的代码的精简版本:

数据库.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 回答