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

时间:2022-12-21 15:59:37

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

所以我试图从我在另一个名为selectColor()的类中创建的方法获取页面上的结果;

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

我似乎无法弄清楚我是否正确行事。在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 个解决方案

#1


0  

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

首先,你应该创建一个类颜色的对象,然后只有访问它的方法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

#1


0  

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

首先,你应该创建一个类颜色的对象,然后只有访问它的方法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