DATA VIEW FROM MYSQL DATABASE THROUGH PDO PROCESS
//LOGICAL TEMPLATE (CLASS || student.php)
class Student{
protected $db_connect;
public function __construct(){
$host_name = 'localhost';
$user_name = 'root';
$password = '';
$db_name = 'db_test';
try{
$this->db_connect = new PDO("mysql:host={$host_name}; dbname={$db_name}",$user_name,$password);
}catch(PDOException $e){
echo $e->getMessage();
}
}
public function select_all_student_info(){
try{
$student_info = $this->db_connect->prepare("SELECT * FROM tbl_student");
$student_info->execute();
return $student_info;
}catch(PDOException $e){
echo $e->getMessage();
}
}
}
//VIEW PAGE
<?php
require_once('student.php');
$obj_student = new Student();
$student_info = $obj_student->select_all_student_info();
?>
<fieldset>
<legend>View Student Form</legend>
<table border="1" cellpadding="15">
<tr>
<td>Student ID</td>
<td>Student Name</td>
<td>Phone Number</td>
<td>Email Address</td>
</tr>
<?php while($row = $student_info->fetch(PDO::FETCH_ASSOC)){ ?>
<tr>
<td><?php echo $row['student_id']; ?></td>
<td><?php echo $row['student_name']; ?></td>
<td><?php echo $row['email_address']; ?></td>
<td><?php echo $row['phone_number']; ?></td>
</tr>
<?php } ?>
</table>
</fieldset>
class Student{
protected $db_connect;
public function __construct(){
$host_name = 'localhost';
$user_name = 'root';
$password = '';
$db_name = 'db_test';
try{
$this->db_connect = new PDO("mysql:host={$host_name}; dbname={$db_name}",$user_name,$password);
}catch(PDOException $e){
echo $e->getMessage();
}
}
public function select_all_student_info(){
try{
$student_info = $this->db_connect->prepare("SELECT * FROM tbl_student");
$student_info->execute();
return $student_info;
}catch(PDOException $e){
echo $e->getMessage();
}
}
}
//VIEW PAGE
<?php
require_once('student.php');
$obj_student = new Student();
$student_info = $obj_student->select_all_student_info();
?>
<fieldset>
<legend>View Student Form</legend>
<table border="1" cellpadding="15">
<tr>
<td>Student ID</td>
<td>Student Name</td>
<td>Phone Number</td>
<td>Email Address</td>
</tr>
<?php while($row = $student_info->fetch(PDO::FETCH_ASSOC)){ ?>
<tr>
<td><?php echo $row['student_id']; ?></td>
<td><?php echo $row['student_name']; ?></td>
<td><?php echo $row['email_address']; ?></td>
<td><?php echo $row['phone_number']; ?></td>
</tr>
<?php } ?>
</table>
</fieldset>
Comments
Post a Comment