Wallogit.com
2017 © Pedro Peláez
This class provides a common method of performing simple CRUD operations on any database supported by PHP PDO
PHP CRUD Lib 2.0 - 9 April 2015, (*1)
By Mehdi Rochdi, (*2)
PHP Class/MySQL use, create, all, update and delete and other functions. It uses PDO driver it's capable to interacting with your mysql database, with easy methods inspire since framworks (cakePHP). you can integrate into you OOP architecture, (*3)
Clone the repository, (*4)
git clone https://github.com/mehdirochdi/php-crud-V2.git
Download composer:, (*5)
curl -sS https://getcomposer.org/installer | php
Install vendors:, (*6)
php composer.phar install
You will need to change some variable in config.php, for your own Database local and a distance, (*7)
"db_host" => "localhost", // change as required "db_user" => "username", // change as required "db_pass" => "password", // change as required "db_name" => "database_name", // change as required
Test Mysql, (*8)
Start by creating test table in your Database, (*9)
CREATE TABLE IF NOT EXISTS `authors` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`emails` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
INSERT INTO posts VALUES('', 'Name 1', 'name 1@email.com');
INSERT INTO posts VALUES('', 'Name 2', 'name 2@email.com');
INSERT INTO posts VALUES('', 'Name 3', 'name 3@email.com');
INSERT INTO posts VALUES('', 'Name', 'name 4@email.com');
table = 'authors'; // Table name
$response = $db->create([
'name' => 'Name 5',
'email' => 'name 5@email.com',
]);
echo 'ID : '.$db->lastInsertId(); // Last insert ID
var_dump($response);
?>
table = 'authors'; // Table name $response = $db->all(); var_dump($response); ?>
table = 'authors'; // Table name
$response = $db->all([
'order' => ['id' => 'DESC']
]);
var_dump($response);
?>
table = 'authors'; // Table name
$response = $db->read(true,4, [
'order' => ['id' => 'DESC']
]);
echo 'The count of row for each page is : '.$db->rowCount().'
';
echo 'The total count of rows is : '.$db->countStatement;
var_dump($response);
// Display Pagination
for($i=1; $i_paginate_number; $i++){
if($i == $db->_paginate_currentPage){
echo ' / '.$i;
}else{
echo ' / '.$i.'';
}
}
?>
table = 'authors'; // Table name $response->$db->findById(1, 'obj'); // num || both || assoc || obj var_dump($response); ?>
table = 'authors'; // Table name
$response->$db->find('all', [
'fields' => ['name', 'emails'],
'conditions' => ['id' => '?'],
'order' => ['id' => 'DESC']
], ['2']);
var_dump($response);
?>
CREATE TABLE IF NOT EXISTS `posts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`author_id` int(11) NOT NULL,
`title` varchar(255) NOT NULL,
`description` text NOT NULL,
`is_actived` tinyint(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
INSERT INTO posts VALUES('', 1, 'Post name 1', 'my description for post 1');
INSERT INTO posts VALUES('', 1, 'Post name 1', 'my description for post 2');
INSERT INTO posts VALUES('', 1, 'Post name 1', 'my description for post 3');
INSERT INTO posts VALUES('', 2, 'Post name 1', 'my description for post 4');
INSERT INTO posts VALUES('', 2, 'Post name 1', 'my description for post 5');
INSERT INTO posts VALUES('', 2, 'Post name 1', 'my description for post 6');
INSERT INTO posts VALUES('', 1, 'Post name 1', 'my description for post 7');
INSERT INTO posts VALUES('', 1, 'Post name 1', 'my description for post 8');
find('all', [
'table' => ['posts' => 'pos'],
'fields' => ['pos.id', 'pos.title','pos.description', 'auth.name '],
'joins' => [
'tables' => ['authors'],
'alias' => ['auth'],
'type' => ['LEFT'],
'condition' => ['auth.id' => 'pos.author_id']
],
'conditions' => ['author_id' => '?'],
'order' => ['pos.id' => 'DESC']
], ['1']
);
echo 'The count of row for each page is : '.$db->rowCount();
var_dump($response);
?>
find('pagination', [
'table' => ['posts' => 'pos'],
'fields' => ['pos.id', 'pos.title','pos.description', 'auth.name '],
'joins' => [
'tables' => ['authors'],
'alias' => ['auth'],
'type' => ['LEFT'],
'condition' => ['auth.id' => 'pos.author_id']
],
'order' => ['pos.id' => 'DESC']
]
);
echo 'The count of row for each page is : '.$db->rowCount().'
';
echo 'The total count of rows is : '.$db->countStatement;
var_dump($response);
// Display Pagination
for($i=1; $i_paginate_number; $i++){
if($i == $db->_paginate_currentPage){
echo ' / '.$i;
}else{
echo ' / '.$i.'';
}
}
?>
table = 'authors'; // Table name
$response = $db->update([
'fields' => [
'name' => 'My Name four',
],
'conditions' => ['id' => '?']
], [4]);
var_dump($response);
?>
table = 'authors'; // Table name $response = $db->deleteById(5); var_dump($response); ?>
Delete with conditions, (*10)
table = 'authors'; // Table name $response = $db->delete(['id' => 5]); var_dump($response); ?>
The PHP CRUD is open-source licensed under the GNU license., (*11)