Php 请在我的回答中告诉你。 class pdoDB { /*** Declare instance ***/ private static $db = NULL; /** * * the constructor is set to private so *

Php 请在我的回答中告诉你。 class pdoDB { /*** Declare instance ***/ private static $db = NULL; /** * * the constructor is set to private so *,php,class,pdo,Php,Class,Pdo,请在我的回答中告诉你。 class pdoDB { /*** Declare instance ***/ private static $db = NULL; /** * * the constructor is set to private so * so nobody can create a new instance using new * */ private function __construct() { /*** maybe set the db name here later

请在我的回答中告诉你。
class pdoDB {

/*** Declare instance ***/
private static $db = NULL;

/**
*
* the constructor is set to private so
* so nobody can create a new instance using new
*
*/
private function __construct() {
/*** maybe set the db name here later ***/
}

/**
*
* Return DB instance or create intitial connection
*
* @return object (PDO)
*
* @access public
*
*/
public static function getInstance() {

 if (!self::$db)
{
self::$db = new PDO('mysql:host=localhost;dbname=attendance_ksu','root','root');;
self::$db-> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
return self::$db;
}

/**
*
* Like the constructor, we make __clone private
* so nobody can clone the instance
*
*/
private function __clone(){
}

} /*** end of class ***/
class Section  {


// if protected can't loop through on html page into table
    // I don't want to make getter method for each attribute.
    // all data will be coming from the db

public $id;
public $section_number;
public $subject;
public $classroom;
public $level_id;
public $shift;
public $course;
public $campus;
public $times;
public $academic_year;
public $start_date;
public $end_date;
public $max_st_number;
public $status;

// retrieve all sections in db

public static function get_all_sections() {

try {

    // maybe a waste of time doing this bit?? not sure!! the " if(!isset($db)) "

if(!isset($db)) {
$db = pdoDB::getInstance();

/*** echo a message saying we have connected for testing reasons only ***/
echo 'Connected to database <br />';
}

/*** The SQL SELECT statement ***/
$sql = "SELECT * FROM sections";
$stmt = $db->query($sql);
$objs = $stmt->fetchALL(PDO::FETCH_CLASS, 'Section');

// 1. return the complete array of data so I can loop through it later

return $objs;  

// 2. Or echo out a loop immediately where this method is called

   /*

foreach ($objs as $section) {
echo "Section:" . $section->section_number . " | " . 
     "Subject: {$section->subject}" . "<br/>";
}

    */

$db = null;   
    }

catch(PDOException $e)
{
echo $e->getMessage();
}

}


// Get the section details by id from db
// will be passed via the URL as $_GET

public static function get_section_by_id($id) {

try {

if(!isset($db)) {
$db = pdoDB::getInstance();

/*** echo a message saying we have connected ***/
echo 'Connected to database<br />';
}   
     /*** prepare the SQL statement ***/
$stmt = $db->prepare("SELECT * FROM sections WHERE id = :id");

/*** bind the paramaters ***/
$stmt->bindParam(':id', $id, PDO::PARAM_INT);

/*** execute the prepared statement ***/
$stmt->execute();

/*** fetch the results and give it a class ***/
$section = $stmt->fetchObject('section');
return $section;

$db = null;   
    }

catch(PDOException $e)
{
echo $e->getMessage();
}   

}
}
<?php
// some code
$sections = Section::get_all_sections();

// any other necessary code
?>

<html> 
<body>

    <table class="table table-striped table-bordered table-highlight" id="example">
<thead>
<tr>
<th>#</th>
<th>Section Number</th>
<th>Subject</th>
<th>Course</th>
<th>Students</th>
<th>Level</th>
<th>Campus</th>
<th>Classroom</th>
<th>Shift</th>
<th>Start date</th>
<th>End date</th>
<th>Teacher</th>
<th>&nbsp;</th>
</tr>
</thead>

<tbody>
<?php 
$key =  1;
foreach($sections as $section) {
?>
<tr>
<td><? echo $key++ ?></td>
<td><a href=""><?php echo $section->section_number ?></a> </td>
<td><?php echo $section->subject ?> </td>
<td><?php echo $section->course ?> </td>
<td><?php echo $section->no_students ?> sts</td>
<td><?php echo $section->level_id ?> </td>
<td><?php echo $section->campus ?> </td>
<td> <?php echo $section->classroom ?></td>
<td><?php echo $section->shift ?> </td>
<td><?php echo date("d-M-Y",strtotime($section->start_date ))?></td>
<td><?php echo date("d-M-Y",strtotime($section->end_date ))?></td>
<td>

<?php 
if($section->username  =="")
{
echo "<a href=\"add_section_teacher.php?id=".urlencode($section->id )."&section_number=".urlencode($section->section_number )."\">Not assigned</a>";

} else {
echo "<a href=\"edit_section_teacher.php?ts_id=".urlencode($section->ts_id )."\">";
echo $section->username ; 
echo "</a>";
}       
?>  
</td>
<td><a href="delete.php?id=<?php echo $section->id ; ?>" 
onclick="return confirm('Are you sure you want to delete section <?php echo $section->section_number ; ?> ?');">Delete</a></td>
</tr>
<? }?></tbody>
</table>
class SectionRepository{
    public function findAll(){
       $db = pdoDB::getInstance();
       $result = $bd->query(SELECT * FROM sections);
       return $result->fetchAll(PDO::FETCH_CLASS,Section);  
    }
}
class PDOFactory{
    private static $connection = null;
    public static function getConnection(){
        if(self::$connection === null){
             self::$connection = new PDO();//here you can use a config file if you want
             self::$connection -> setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
        }
        return self::$connection;
}