doujiexin1136 2014-05-03 02:37
浏览 39
已采纳

从php中的另一个类获取函数

So I am trying to just get the results on a page from a method I created in another class called selectColor();

I cant seem to figure out if I am doing this right. Is there a special way to do this in PDO?

connect.php - connection to database

<?php 
    //Initialize variables to make db connection
    $host = "localhost";
    $dbName = "stormfront_productions_test";
    $username = "root";
    $password = "root";

    try{
        $pdo = new PDO("mysql:dbname=$dbName;host=$host", $username, $password);
        echo 'Connection Successful';
    }catch(PDOException $e){
        $error = $e->getMessage();
        echo $error;
    }




?>

colors.class.php--all of the functions

  <?php



class colors {

//SELECT
    function selectColor()
    {
        $sql= "SELECT * FROM colors";
        require 'model/connect.php';
        $stmt = $pdo->query($sql); 
        $row =$stmt->fetchObject();
        echo "<table><tr><td>" . $row->id . "</td>";
        echo "<td>" . $row->color . "</td></tr></table>";
    }

//INSERT
    function insertColor($color){
        $sql = "INSERT INTO colors(color) VALUES (
            :color"; 
        require 'model/connect.php';
        $stmt = $pdo->prepare($sql);                                 
        $stmt->bindParam(':color', $color);       
        $stmt->execute();
    }


//UPDATE
    function updateColor($color){
        $sql = "UPDATE colors SET color = :color; 
            WHERE id = :id";
        require 'model/connect.php';
        $stmt = $pdo->prepare($sql);                                  
        $stmt->bindParam(':color', $color);       
        $stmt->execute();        
    }


//DELETE
    function deleteColor($color){
        $sql = "DELETE FROM colors WHERE color =  :color";
        require 'model/connect.php';
        $stmt = $pdo->prepare($sql);
        $stmt->bindParam(':color', $color);   
        $stmt->execute();
    }
}// END OF colors class

colors.php--shows the results of the function selectColor();

   <!DOCTYPE html>
<?php include 'model/functions/colors.class.php';
      include '/model/connect.php'?>

<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
         <?php
         $getConn = new $pdo();
    $showSelect = new colors();
    $showSelect->selectColor();
    ?>
    </body>
</html>
  • 写回答

1条回答 默认 最新

  • duandou1903 2014-05-03 02:40
    关注

    At first you should create an object of class colors and only after that access it's method selectColor() :

     <body>
        <?php
        $showSelect = new colors();
        $showSelect->selectColor();
        ?>
    </body>
    

    PHP :

    use PDO;
    
    class colors {
    
    private $pdo;
    
    public function __construct(){
       $host = "localhost";
       $dbName = "stormfront_productions_test";
       $username = "root";
       $password = "root";
    
       try{
          $this->pdo = new PDO("mysql:dbname=$dbName;host=$host", $username, $password);
          echo 'Connection Successful';
       }catch(PDOException $e){
          $error = $e->getMessage();
          echo $error;
       }
    }
    
    //SELECT
    function selectColor()
    {
        $sql= "SELECT * FROM colors";
        $stmt = $this->pdo->query($sql); 
        $row =$stmt->fetchObject();
        echo "<table><tr><td>" . $row->id . "</td>";
        echo "<td>" . $row->color . "</td></tr></table>";
    }
    
    //INSERT
    function insertColor($color){
        $sql = "INSERT INTO colors(color) VALUES (
            :color"; 
        $stmt = $this->pdo->prepare($sql);                                 
        $stmt->bindParam(':color', $color);       
        $stmt->execute();
    }
    
    
    //UPDATE
    function updateColor($color){
        $sql = "UPDATE colors SET color = :color; 
            WHERE id = :id";
        $stmt = $this->pdo->prepare($sql);                                  
        $stmt->bindParam(':color', $color);       
        $stmt->execute();        
    }
    
    
    //DELETE
    function deleteColor($color){
        $sql = "DELETE FROM colors WHERE color =  :color";
        $stmt = $this->pdo->prepare($sql);
        $stmt->bindParam(':color', $color);   
        $stmt->execute();
    }
    } // END OF colors class
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集