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
Here is code for User class :
<?php
Now create a new object of User class :
<?php
requirements.
Here i will explain methods which are already i created for above four operations.
<?php
1. Insert
$custom = $user->customQuery($query);
?>
For detailed description about PHP PDO please visit php.net
First Download files for your local server from github . Now
- Update config.php for database connection.
- Create a new table (i.e. users) in database.
- Create a new (i.e. User) class in index.php file which will extend "Connection" class .
- Here Connection is class which is used for methods implementatios.
- 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 ??
- Insert
- Update
- Delete and
- Select
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)"; |
Comments
Post a Comment