所以我试图从我在另一个名为selectColor()的类中创建的方法获取页面上的结果;
我似乎无法弄清楚我是否正确行事。在PDO中有一种特殊的方法吗?
<?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;
}
?>
<?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
<!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>
答案 0 :(得分:0)
首先,你应该创建一个类colors
的对象,然后才能访问它的方法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