Skip to main content

PHP PDO implementation using class

The PHP Data Objects (PDO) extension defines a lightweight, consistent interface for accessing databases in PHP.

For detailed description about PHP PDO please visit php.net 




First Download files for your local server from  github .  Now
  1. Update config.php for database connection.
  2. Create a new table (i.e. users) in database.
  3. Create a new (i.e.  User) class in index.php file which will extend "Connection" class . 
  4. Here Connection is class which is used for methods implementatios.
  5. Call parent constructor from the class you created.

Here is code for User class :

 <?php
class User extends Connection {
     public function __construct(){
         parent::__construct();
     }
}
?> 
 
Why called  parent construct ??   I used php pdo database connection in "Connection" class constructor method which will be called when object of class will initialize.
Now create a new object of User class :

<?php
$user  = new User;
?> 

What next ??
  1. Insert
  2. Update
  3. Delete  and
  4. Select
You can check class.pdo.php ,  modify any methods or  add new methods according to your
requirements.

Here i will  explain methods which are already i created for above four operations.


<?php

1. Insert

$insert = $user->insertData('tablename',array(
'fields'=>array('col1','col2'),
'values'=>array('val1','val2')
));
echo $insert; // to check response

2. Update
$update = $user->updateData('tablename',array(
'fields'=>array('col1','col2'),
'values'=>array('val1','val2'),
'and'=>array('col1'=>'val1','col2'=>'val2')
));
echo $update; // to check response

Here and is an array for all conditions with "AND" in update query.

For "OR" conditions in Update array will be :
$update = $user->updateData('tablename',array(
'fields'=>array('col1','col2'),
'values'=>array('val1','val2'),
'or'=>array('col1'=>'val1','col2'=>'val2')
));
echo $update; // to check response

To use "LIKE" or "NOT EQUAL" Operator Simply append text after column name in array.

For Example and condition with "LIKE" and "NOT EQUAL" will be :
$update = $user->updateData('tablename',array(
'fields'=>array('col1','col2'),
'values'=>array('val1','val2'),
'or'=>array('col1 LIKE'=>'val1','col2 !='=>'val2')
));
echo $update; // to check response

3. Delete
$delete = $user->deleteData('tablename',array(
'and'=>array('col1'=>'val1','col2' => 'val2')
));
echo $delete; // to check response
Like Update in delete or array also can be passed for "OR" in where conditions.

4.Select
$data = $user->getData('tablename'); //To fetch all data from table.

$data = $user->getData('tablename', array('single'=>true); // to fetch last inserted data

$data = $user->getData('tablename',array('fields'=>array('col1','col2','col3','col4'))); // to fetch only selected columns from table

$data = $user->getData('tablename', array('or'=>array('col1 like'=>'val1','col2'=>'val2'))); // with or conditions

$data = $user->getData('tablename', array('and'=>array('col1 like'=>'val1','col2'=>'val2'))); // with and conditions

$data = $user->getData('tablename', array('order'=>array('column'=>'desc'))); // with order by

$dataById = $user->getDataById('tablename',$id,array('fields'=>array('col1','col2','col3','col4'))); // get table data by #ID


CUSTOM SELECT QUERY
$query = "put your custom query here (for single table or join query)";
$custom = $user->customQuery($query);

?>

Comments

Popular posts from this blog

Getting Started With Python - for PHP developers

This blog is not specially for PHP developers. So don't worry if you are not from PHP Development Background. Recommended Reading - IBM - Python basics for PHP developers After reading above article i am assuming you already knows A) What is Python. B) How Python is different from PHP (If you are from PHP Background) C) Basic understanding for variables in Python. This Tutorial will cover Web Application Development using Python and  MySQL . Prerequisite  1. Install  MySQL   for database storage.  2. Install Python .  3. Install Django .    why Django ?   Yess right question.  Django is open source web framework which is most popular to create a web application in python language. No doubt you can start development by core python. But if you are going to develop a secure standard application than Django is a boiler plate to get started quickly. Except  Django   there are also some python w...

Blog CMS Admin Panel Using Laravel within 10 minutes

Hey !! Welcome Back. I am using latest version    Laravel 5.4   for  this post. Before continue please check your are updated with your PHP version required for the running environment of  Laravel. And you have installed the  Composer  in your system. You can check & verify  your running environment from  here . If you are up to date with the minimum requirement than lets continue. Open your terminal and type enter after below command : composer create - project -- prefer - dist laravel / laravel blog   Now wait for a moment. If you got any error during installation than terminal will warn you. Please  Google   the issue and resolve it.  If you are still facing any issue for the same just comment below. If you installed the laravel  successfully than in your system than lets move to next step. There will be an new folder " blog "  where you executed the above composer command.  Just...