Wednesday 17 October 2018

Database Class: MYSQLI CRUD OOP Series

The following steps will walk us through in creating a system with simple CRU (Create, Read and Update) implementation in PHP Class. This Class Notes was made possible in collaboration with Professor Jessie Richie Naval de los Santos handling IT Elective (PHP) Class. The source codes was documented to serve as Notes for students in writing up their system with OOP implementation.

Step 1. Create Database Class
This class will establish the database connection
in every functions of the system that will be created.  Filename: class.db.php
Code:
<?php
class DBconn{
public $dbconn;
public function __construct(){
$server = 'localhost';
$user ='root';
$pass = '';
$dbname = 'exam';
$this->dbconn = mysqli_connect($server,$user,$pass,$dbname);
if (mysqli_connect_errno()){
echo 'Error: Could not connect to database.';
exit;
}else{
//echo 'Connected';
}
}
}
$test = new dbconn();
?>


Discussion:
By default all function in class are automatically assigned as public.
$dbconn variable will be used as the database connection across classes.

0 comments:

Post a Comment

If possible, leave a positive comment. No hate speech or elicit comments. Thank you.