在PHP中,如何将一个PDO连接用于同一页面的不同部分?

时间:2017-05-04 09:41:05

标签: php pdo

我已经在PHP中练习了一段时间,现在我遇到了这个问题。

如何为同一个html页面的不同部分建立一个连接,其中有不同的PHP标记?例如下面是我的连接类。

   // class
        class connection{
           public function dbconnection(){
            $user = "user";
            $pass = "pass";
            $pdo_connection = new PDO('mysql:dbname=shefilco_madimo
            ', $user, $pass);
              return $pdo_connection;               
              }
       }


   <div id="section_1">
    <?php
     include("connection.php");
         class section_one{  
         public function __construct(){
          $this->conn = new connection();
          $this->conn = $this->conn->dbconnection();
    }
     public function fetchdata1(){
          //my codes to fetch data
        }
       $object1=new  post_photos();
       $object1->display_post_photos();
    ?>
    </div>
   <div id="section_2">
      <?php
     include("connection.php");
         class section_one{  
         public function __construct(){
          $this->conn = new connection();
          $this->conn = $this->conn->dbconnection();
    }
     public function fetchdata2(){
          //my codes to fetch data
        }
       $object2=new  post_photos();
       $object2->display_post_photos();
    ?>
   </div>
   <div id="section_3">
       <?php
     include("connection.php");
         class section_one{  
         public function __construct(){
          $this->conn = new connection();
          $this->conn = $this->conn->dbconnection();
    }
     public function fetchdata3(){
          //my codes to fetch data
        }
       $object3=new  post_photos();
       $object3->display_post_photos();
    ?>
    </div>

      //my page that has different sections
     And the  connection class :

0 个答案:

没有答案