<i id='3uzuX'><tr id='3uzuX'><dt id='3uzuX'><q id='3uzuX'><span id='3uzuX'><b id='3uzuX'><form id='3uzuX'><ins id='3uzuX'></ins><ul id='3uzuX'></ul><sub id='3uzuX'></sub></form><legend id='3uzuX'></legend><bdo id='3uzuX'><pre id='3uzuX'><center id='3uzuX'></center></pre></bdo></b><th id='3uzuX'></th></span></q></dt></tr></i><div id='3uzuX'><tfoot id='3uzuX'></tfoot><dl id='3uzuX'><fieldset id='3uzuX'></fieldset></dl></div>

<legend id='3uzuX'><style id='3uzuX'><dir id='3uzuX'><q id='3uzuX'></q></dir></style></legend>

  1. <tfoot id='3uzuX'></tfoot>
  2. <small id='3uzuX'></small><noframes id='3uzuX'>

        <bdo id='3uzuX'></bdo><ul id='3uzuX'></ul>

      扩展 PDO 类

      时间:2023-10-04
        • <legend id='Wbzwq'><style id='Wbzwq'><dir id='Wbzwq'><q id='Wbzwq'></q></dir></style></legend>
          • <bdo id='Wbzwq'></bdo><ul id='Wbzwq'></ul>

            <i id='Wbzwq'><tr id='Wbzwq'><dt id='Wbzwq'><q id='Wbzwq'><span id='Wbzwq'><b id='Wbzwq'><form id='Wbzwq'><ins id='Wbzwq'></ins><ul id='Wbzwq'></ul><sub id='Wbzwq'></sub></form><legend id='Wbzwq'></legend><bdo id='Wbzwq'><pre id='Wbzwq'><center id='Wbzwq'></center></pre></bdo></b><th id='Wbzwq'></th></span></q></dt></tr></i><div id='Wbzwq'><tfoot id='Wbzwq'></tfoot><dl id='Wbzwq'><fieldset id='Wbzwq'></fieldset></dl></div>
              <tbody id='Wbzwq'></tbody>
              <tfoot id='Wbzwq'></tfoot>

                <small id='Wbzwq'></small><noframes id='Wbzwq'>

              1. 本文介绍了扩展 PDO 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                以下是我目前推出的数据库连接类,但我将通过扩展 PDO 类本身来改进它,

                Below is the db connection class I came out with so far, but I am going to improve it by extending the PDO class itself,

                <?php
                class database
                {
                    protected $connection = null;
                
                    #make a connection
                    public function __construct($hostname,$dbname,$username,$password)
                    {
                        try 
                        {
                            # MySQL with PDO_MYSQL  
                            $this->connection = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
                            $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
                        }
                        catch (PDOException $e) 
                        {
                            $this->connection = null;
                            die($e->getMessage());
                        }
                    }
                
                    #get the number of rows in a result
                    public function num_rows($query)
                    {
                        # create a prepared statement
                        $stmt = $this->connection->prepare($query);
                
                        if($stmt) 
                        {
                            # execute query 
                            $stmt->execute();
                
                            return $stmt->rowCount();
                        } 
                        else
                        {
                            return self::get_error();
                        }
                    }
                
                    #display error
                    public function get_error() 
                    {
                        $this->connection->errorInfo();
                    }
                
                    # closes the database connection when object is destroyed.
                    public function __destruct()
                    {
                        $this->connection = null;
                    }
                }
                ?>
                

                扩展类,

                class database extends PDO
                {
                
                    #make a connection
                    public function __construct($hostname,$dbname,$username,$password)
                    {
                        parent::__construct($hostname,$dbname,$username,$password);
                
                        try 
                        { 
                            $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
                        }
                        catch (PDOException $e) 
                        {
                            die($e->getMessage());
                        }
                    }
                
                    #get the number of rows in a result
                    public function num_rows($query)
                    {
                        # create a prepared statement
                        $stmt = parent::prepare($query);
                
                        if($stmt) 
                        {
                            # execute query 
                            $stmt->execute();
                
                            return $stmt->rowCount();
                        } 
                        else
                        {
                            return self::get_error();
                        }
                    }
                
                    #display error
                    public function get_error() 
                    {
                        $this->connection->errorInfo();
                    }
                
                    # closes the database connection when object is destroyed.
                    public function __destruct()
                    {
                        $this->connection = null;
                    }
                }
                

                这就是我实例化类的方式,

                This is how I instantiate the class,

                # the host used to access DB
                define('DB_HOST', 'localhost');
                
                # the username used to access DB
                define('DB_USER', 'root');
                
                # the password for the username
                define('DB_PASS', 'xxx');
                
                # the name of your databse 
                define('DB_NAME', 'db_2011'); 
                
                include 'class_database.php';
                
                $connection = new database(DB_HOST,DB_NAME,DB_USER,DB_PASS);
                $sql = "
                    SELECT *
                    FROM root_contacts_cfm
                    ORDER BY cnt_id DESC
                    ";
                
                $connection->num_rows($sql);
                

                但是当我调用这个扩展的 pdo 类时出现错误,

                But I have errors when I call this extended pdo class,

                警告:PDO::__construct() 需要参数 4 为数组,给出字符串在 C:wampwwwxxclass_database.php在线 xx

                Warning: PDO::__construct() expects parameter 4 to be array, string given in C:wampwwwxxclass_database.php on line xx

                致命错误:调用成员函数setAttribute() 在非对象上C:wampwwwxxclass_database.php 上第 xx 行

                Fatal error: Call to a member function setAttribute() on a non-object in C:wampwwwxxclass_database.php on line xx

                我在网上做了一些研究,我找到了扩展pdo的这个基本结构,但我不明白......

                I have done some research online, I found this basic structure of extending pdo but I dont understand it...

                class myPDO extends PDO
                {
                   public function __construct($dsn, 
                                               $username=null, 
                                               $password=null, 
                                               $driver_options=null)
                   {
                      parent::__construct($dsn, $username, $password, $driver_options);
                   }
                
                   public function query($query)
                   {
                      $result = parent::query($query);
                      // do other stuff you want to do here, then...
                      return($result);
                   }
                }
                

                $dsn 变量有什么用?如何将我的 $hostname 变量传递给扩展的 pdo 类?

                What is $dsn variable for? How can I pass my $hostname variable into extended pdo class?

                另一个问题:如何在扩展的 pdo 类中创建一个显示错误的方法?如何关闭扩展 pdo 类中的连接?

                Another questions: How can I make a method for displaying error in the extended pdo class? How can I close the connection in the extended pdo class?

                从mysqli迁移到pdo太难了!

                It is so difficult to move from mysqli to pdo!

                谢谢.

                推荐答案

                $dsn 是数据源名称.它为您处理您的主机名.你像这样使用它:

                $dsn is data source name. It handles your hostname for you. You use it like this:

                $dsn = 'mysql:dbname=YOUR_DB_NAME;host=YOUR_HOSTNAME'
                

                随着行 $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 你已经设置了在发生错误时引发的异常(我喜欢),所以在你的扩展类,您可以处理异常处理程序中的错误.如果您的扩展 PDO 类中有一个名为 getAssoc 的方法,那么它看起来像这样:

                With the line $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); You have set exceptions to be raised when errors occur (which I like), so in your extended class you can handle errors in exception handlers. If you had a method called getAssoc in your extended PDO class then it would look like this:

                /// Get an associative array of results for the sql.
                public function getAssoc($sql, $params=array())
                {
                   try
                   {
                      $stmt = $this->prepare($sql);
                      $params = is_array($params) ? $params : array($params);
                      $stmt->execute($params);
                
                      return $stmt->fetchAll(PDO::FETCH_ASSOC);
                   }
                   catch (Exception $e)
                   {
                      // Echo the error or Re-throw it to catch it higher up where you have more
                      // information on where it occurred in your program.
                      // e.g echo 'Error: ' . $e->getMessage(); 
                
                      throw new Exception(
                            __METHOD__ . 'Exception Raised for sql: ' . var_export($sql, true) .
                            ' Params: ' . var_export($params, true) .
                            ' Error_Info: ' . var_export($this->errorInfo(), true),
                            0,
                            $e);
                   }
                }
                

                这篇关于扩展 PDO 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:带有 MySQL 插入 PDO 请求的错误 HY093 下一篇:存储/检索 PGP 私钥和密码的安全方法?

                相关文章

                最新文章

                <small id='vcVcF'></small><noframes id='vcVcF'>

                <i id='vcVcF'><tr id='vcVcF'><dt id='vcVcF'><q id='vcVcF'><span id='vcVcF'><b id='vcVcF'><form id='vcVcF'><ins id='vcVcF'></ins><ul id='vcVcF'></ul><sub id='vcVcF'></sub></form><legend id='vcVcF'></legend><bdo id='vcVcF'><pre id='vcVcF'><center id='vcVcF'></center></pre></bdo></b><th id='vcVcF'></th></span></q></dt></tr></i><div id='vcVcF'><tfoot id='vcVcF'></tfoot><dl id='vcVcF'><fieldset id='vcVcF'></fieldset></dl></div>
                <legend id='vcVcF'><style id='vcVcF'><dir id='vcVcF'><q id='vcVcF'></q></dir></style></legend>
                  <bdo id='vcVcF'></bdo><ul id='vcVcF'></ul>
                <tfoot id='vcVcF'></tfoot>