• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
seegatesite header

Seegatesite.com

Seegatesite.com - Programming Tutorial , Sharing , How and Learn Together

  • TOOLS
    • Bootstrap Navbar Online Generator
    • Customize Sidebar Menu Bootstrap 3
    • Bootstrap Demo
  • ADVERTISE
  • CATEGORIES
    • Android
    • Blogging Tips
    • Database
    • CSS
    • Info Gadget
    • Javascript
    • Linux
    • PHP
    • Various
    • WordPress
  • Q&A
  • PHP
  • JAVASCRIPT
  • JQUERY
  • ANGULAR
  • WORDPRESS
  • SEO
  • REACT
🏠 » PHP » Tutorial PHP Class For Simple CRUD with PDO For Beginners

Tutorial PHP Class For Simple CRUD with PDO For Beginners

By Sigit Prasetya Nugroho ∙ May 1, 2016 ∙ PHP ∙ 1 Comment

Share : TwitterFacebookTelegramWhatsapp

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

    • 0.1 Definition of PDO (PHP Data Objects)
  • 1 Let’s start the tutorial PHP class to create crud with PDO extention.
    • 1.1 Hope these article about Tutorial PHP Class For Simple CRUD with PDO For Beginners useful 🙂

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:

Tutorial PHP Class For Simple CRUD With PDO For Beginners

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”

Create Database Crud And Table Person Tutorial PHP Class For Simple CRUD With PDO For Beginners

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

Result Tutorial PHP Class For Simple CRUD With PDO For Beginners

If you need a source code tutorial php class for simple crud using pdo , please download from the following link
[sociallocker id=”58″]
php class pdo download link [/sociallocker]

Hope these article about Tutorial PHP Class For Simple CRUD with PDO For Beginners useful 🙂

Another PHP Related Post :

  • Tutorial Create Simple POS Using ReactJS And Laravel Lumen Part 1
  • How To Replace String With Another String In PHP
  • Login Page – Tutorial CRUD Client and API Server Using JQuery And Lumen Part 2
  • Tutorial CRUD Client and API Server Using JQuery And Lumen Part 1
  • How To Solve Problems Illegal mix of collations (latin1_swedish_ci,IMPLICIT) In Laravel
  • How To Resolve No ‘Access-Control-Allow-Origin’ Header In Lumen

Avatar for Sigit Prasetya Nugroho

About Sigit Prasetya Nugroho

This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.

Reader Interactions

Comments

  1. Avatar for maumau says

    August 28, 2017 at 9:36 pm

    password rar file ?

    Reply
    • Avatar for Sigit Prasetya NugrohoSigit Prasetya Nugroho says

      August 29, 2017 at 2:04 am

      there is no password in my zip file 🙂

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Welcome to my Home,

Avatar for Sigit Prasetya NugrohoThis site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.



Popular Articles

Checked checkbox AdminLTE Bootstrap in Jquery

November 4, 2014 By Sigit Prasetya Nugroho 7 Comments

Simple create date format validation with jqueryUI

December 21, 2014 By Sigit Prasetya Nugroho Leave a Comment

Create Simple Progress Bar for Fake Online Generator with Jquery

January 10, 2015 By Sigit Prasetya Nugroho Leave a Comment

22+ Coolest Free Jquery Plugin For Premium Theme

October 3, 2015 By Sigit Prasetya Nugroho Leave a Comment

Easy Build Your Anti Copy Paste Plugin

October 6, 2015 By Sigit Prasetya Nugroho Leave a Comment

Popular Tags

adminlte (15) adsense (13) adsense tips (4) affiliate amazon (13) amazon (12) Android (8) angular (16) angular 4 (12) angular 5 (4) asin grabber (3) Bootstrap (27) codeigniter (5) create wordpress theme (5) crud (8) css (6) free wordpress theme (7) google adsense (4) imacros (4) increase traffic (6) jquery (34) laravel (10) laravel 5 (5) learn android (5) lumen api (4) modal dialog (5) mysql (6) nodeJs (4) optimize seo (4) pdo (6) php (30) plugin (53) pos (8) Publisher Tips (5) react (4) Reactjs (7) SEO (37) theme (17) tutorial angular (5) tutorial angular 4 (6) tutorial javascript (10) tutorial javascript beginners (4) twitter (3) wordpress (18) wordpress plugin (13) XMLRPC (5)




  • About
  • Contact Us
  • Disclaimer
  • Privacy Policy
  • Terms and Conditions

©2021 Seegatesite.com