Tutorial PHP Class For Simple CRUD with PDO For Beginners

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.

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

Create PHP Class for Database Connection with PDO

Create a php file with the name dbconnect.php and copy the following code :

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

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

6. truncate table person

$crud->emptyTablePerson();

Whole of script, create new php file as index.php and copy the following code :

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

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

The following two tabs change content below.
This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.

2 Comments

 Add your comment
  1. password rar file ?

Leave a Comment

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.