Seegatesite.com – Step by step tutorial create php class for simple CRUD with PDO. using PDO in PHP turned out to be much easier and simpler than with mysql extention. Starting from PHP5 upgrade to Php7, requires me to learn more about the PHP PDO extension. Php7 no longer supports MySQL extension, so inevitably have to switch to PDO. Sometimes when we hear the word PDO Class, we give up yet. Apparently, using the PDO is much more comfortable and effective. I will share a tutorial php class for simple crud with pdo. To be clear, I will discuss a little bit in advance what is the PHP PDO.
Table of Contents
Definition of PDO (PHP Data Objects)
PDO (PHP Data Objects) is a universal interface that is provided by PHP to “communicate” with the database server. You mean the term “universal interface” here is that the PDO is not tied to specific database application. If today we use the MySQL database and later want to migrate using PostgreSQL, we only stayed change how the initial call PDO and all the existing code can be directly used for the new database (this is one advantage phenomenal PDO).
Technically, if using PDO, it is not connected directly to the database, but only as an “interface”. PDO concept can be described as follows:
PDO working with a method called “a data-access abstraction layer”. That is, any type of database server that used, written PHP code will remain the same. PDO provides “abstraction layer” to communicate with the database server.
To use the PDO, we have to access it using objects. PDO does not provide a way of writing style such procedural or mysql mysqli extension (It makes us afraid to try PDO). But here I will share how I make php PDO class to use easily. I once wrote an article about Connect Bootstrap Login Form and MySQL with PDO, but how to use PDO I have not discussed in full.
Let’s start the tutorial PHP class to create crud with PDO extention.
Creating a database “crud” and a table “person”
1 2 3 4 5 6 7 | CREATE TABLE `person` ( `id_person` int(11) NOT NULL AUTO_INCREMENT, `name_person` varchar(100) DEFAULT NULL, `gender` enum('Male','Female') DEFAULT NULL, `country` varchar(50) DEFAULT NULL, PRIMARY KEY (`id_person`) ) |
Create PHP Class for Database Connection with PDO
Create a php file with the name dbconnect.php and copy the following code :
1 2 3 4 5 | <?php class dbconnect { public $dbase; public function initDB() { $this->dbase = new PDO("mysql:host=localhost;dbname=crud;charset=latin1","root","123456",array(PDO::ATTR_PERSISTENT => true)); $this->dbase->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); } } ?> |
Explanation :
PDO("mysql:host=localhost;dbname=crud;charset=latin1","root","123456",array(PDO::ATTR_PERSISTENT => true))
- localhost : database server location, we can change with ip address or domain
- crud : database name
- root : database username
- 123456 : database password
Creating a PHP class to access CRUD with PDO
Create a php file with the name crud.php and copy the following code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | <?php require_once("dbconnect.php"); class crud extends dbconnect { public function __construct() { $this->initDB(); } /*Table Person*/ public function getListPerson(){ $db = $this->dbase; try { $stmt = $db->prepare("select * from person"); $stmt->execute(); $stat[0] = true; $stat[1] = $stmt->fetchAll(PDO::FETCH_ASSOC); return $stat; } catch(PDOException $ex) { $stat[0] = false; $stat[1] = $ex; return $stat; } } public function getPerson($id_person){ $db = $this->dbase; try { $stmt = $db->prepare("select * from person where id_person = :id "); $stmt->bindParam("id",$id_person); $stmt->execute(); $stat[0] = true; $stat[1] = $stmt->fetchAll(PDO::FETCH_ASSOC); return $stat; } catch(PDOException $ex) { $stat[0] = false; $stat[1] = $ex; return $stat; } } public function updatePerson($id_person,$name_person,$gender,$country) { $db = $this->dbase; try { $stmt = $db->prepare("UPDATE person SET name_person = :name ,gender = :gender,country = :country WHERE id_person = :id"); $stmt->bindParam("id",$id_person); $stmt->bindParam("name",$name_person); $stmt->bindParam("gender",$gender); $stmt->bindParam("country",$country); $stmt->execute(); $stat[0] = true; $stat[1] = "Update Successfully"; return $stat; } catch(PDOException $ex) { $stat[0] = false; $stat[1] = $ex; return $stat; } } public function savePerson($name_person,$gender,$country){ $db = $this->dbase; try { $stmt = $db->prepare("INSERT INTO person(name_person,gender,country) VALUES(:name,:gender,:country)"); $stmt->bindParam("name",$name_person); $stmt->bindParam("gender",$gender); $stmt->bindParam("country",$country); $stmt->execute(); $stat[0] = true; $stat[1] = "Save Successfully"; return $stat; } catch(PDOException $ex) { $stat[0] = false; $stat[1] = $ex; return $stat; } } public function deletePerson($id_person) { $db = $this->dbase; try { $stmt = $db->prepare("DELETE from person WHERE id_person = :id"); $stmt->bindParam("id",$id_person); $stmt->execute(); $stat[0] = true; $stat[1] = "Delete Successfully"; return $stat; } catch(PDOException $ex) { $stat[0] = false; $stat[1] = $ex; return $stat; } } public function GetPersonByName($term) { $trm = "%".$term."%"; $db = $this->dbase; try { $stmt = $db->prepare("select * from person where name_person like :term order by name_person asc"); $stmt->bindParam("term",$trm); $stmt->execute(); $stat[0] = true; $stat[1] = $stmt->fetchAll(PDO::FETCH_ASSOC); return $stat; } catch(PDOException $ex) { $stat[0] = false; $stat[1] = $ex; return $stat; } } public function emptyTablePerson() { $db = $this->dbase; try { $stmt = $db->prepare("truncate table person"); $stmt->execute(); $stat[0] = true; $stat[1] = "Delete Successfully"; return $stat; } catch(PDOException $ex) { $stat[0] = false; $stat[1] = $ex; return $stat; } } /******************************************************************************* END OF TABEL Person *******************************************************************************/ } ?> |
How to use php class crud :
1. Running crud php class
require_once("crud.php");
$crud = new crud();
2. Insert new data person
$save = $crud->savePerson("Sigit","Male","Indonesia");
print_r('Save Result = ');
print_r($save);
3. Update data person by id
$update = $crud->updatePerson(5,"Song Hye Kyo","Female","South Korea");
print_r('Update id 5 Result = ');
print_r($update);
4. Delete data person by id
$delete = $crud->deletePerson(2);
print_r('Delete id 2 Result = ');
print_r($delete);
5. Display all data person
1 2 3 4 5 6 7 8 9 10 11 | $getlistperson = $crud->getListPerson(); print_r('List Person : '); echo ' '; echo ' ID | Name | Gender | Country '; echo ' '; foreach ($getlistperson[1] as $key ) { echo $key['id_person'].' | '.$key['name_person'].' | '.$key['gender'].' | '.$key['country']; echo ' '; } |
6. truncate table person
$crud->emptyTablePerson();
Whole of script, create new php file as index.php and copy the following code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | <?php require_once("crud.php"); $crud = new crud(); $crud->emptyTablePerson(); $save = $crud->savePerson("Sigit","Male","Indonesia"); print_r('Save Result = '); print_r($save); print_r("<br>"); $save = $crud->savePerson("John Doe","Male","France"); print_r('Save Result = '); print_r($save); print_r("<br>"); $save = $crud->savePerson("Wang Xi","Male","China"); print_r('Save Result = '); print_r($save); print_r("<br>"); $save = $crud->savePerson("Tatsuya Arigato","Male","Japan"); print_r('Save Result = '); print_r($save); print_r("<br>"); $save = $crud->savePerson("Angela","Female","England"); print_r('Save Result = '); print_r($save); print_r("<br>"); $getlistperson = $crud->getListPerson(); print_r('List Person : '); echo '<br>'; echo ' ID | Name | Gender | Country '; echo '<br>'; foreach ($getlistperson[1] as $key ) { echo $key['id_person'].' | '.$key['name_person'].' | '.$key['gender'].' | '.$key['country']; echo '<br>'; } print_r("<br>"); $update = $crud->updatePerson(5,"Song Hye Kyo","Female","South Korea"); print_r('Update id 5 Result = '); print_r($update); print_r("<br>"); $getlistperson = $crud->getListPerson(); print_r('List Person : '); echo '<br>'; echo ' ID | Name | Gender | Country '; echo '<br>'; foreach ($getlistperson[1] as $key ) { echo $key['id_person'].' | '.$key['name_person'].' | '.$key['gender'].' | '.$key['country']; echo '<br>'; } print_r("<br>"); $delete = $crud->deletePerson(2); print_r('Delete id 2 Result = '); print_r($delete); print_r("<br>"); $delete = $crud->deletePerson(3); print_r('Delete id 3 Result = '); print_r($delete); print_r("<br>"); $getlistperson = $crud->getListPerson(); print_r('List Person : '); echo '<br>'; echo ' ID | Name | Gender | Country '; echo '<br>'; foreach ($getlistperson[1] as $key ) { echo $key['id_person'].' | '.$key['name_person'].' | '.$key['gender'].' | '.$key['country']; echo '<br>'; } ?> |
Run these script from your browser, if successfully will display content like below
If you need a source code tutorial php class for simple crud using pdo , please download from the following link
password rar file ?
there is no password in my zip file 🙂