SimplePDOClass
PHP PDO wrapper
Install / Use
/learn @Roullie/SimplePDOClassREADME
SimplePDOClass
Database Credentials
Open config.php and enter credentials
Calling the Dbconnect class
$db = new Dbconnect();
Select
$db->Select[$table]( );
$table must be capitalized. Example you have a table named users, you should do $db->SelectUsers( ).
Select with conditions
$conditions = array(
'id' => 1,
'created >' => '2016-04-25'
);
$db->SelectUsers( $conditions );
Above will produce the query string
Select * from users where id = '1' and created > '2016-04-25'
Update
$conditions = array(
'id' => 1,
'status' => 2
);
$db->UpdateUsers( $conditions );
Above will produce the query string
update users set status = '2' where id = '1'
Update[$table]() always need an id to work
Update where
$columns = array(
'name' => 'John Doe',
'email' => 'email@email.com'
);
$conditions = array(
'id' => 1,
'status !=' => 2
);
$db->UpdatewhereUsers( $columns , $conditions );
Above will produce the query string
update users set name = 'John Doe', email = 'email@email.com' where id = '1' and status != 2
Delete
$conditions = array(
'id' => 1,
'status' => 2
);
$db->DeleteUsers( $conditions );
Above will produce the query string
Delete from users where status = '2' and id = '1'
Insert
$columns = array(
'status' => 1,
'name' => 'John Doe',
'email' => 'email@email.com',
'created' => '2016-04-25'
);
$db->InsertUsers( $columns );
Above will produce the query string
Insert into users (status,name,email,created) values ('1','John Doe','email@email.com','2016-04-25')
$db->InsertUsers( $columns ); will return the last inserted id
Join
$db
->joins(array(
'type' => 'left',
'table' => 'images',
'on' => 'images.user_id = users.id'
))
->SelectUsers( );
Above will produce the query string
Select * from users left join images on images.user_id = users.id
Default type is left so no need to add it in the options. You can also add an alias to the joins option like
$db
->joins(array(
'type' => 'right',
'table' => 'images',
'as' => 'im',
'on' => 'im.user_id = users.id'
))
->SelectUsers( array(
'im.type' => 3
) );
Above will produce the query string
Select * from users right join images as im on im.user_id = users.id where im.type = '3'
Select with specific columns
$db
->columns(array(
'users.*',
'images.url',
'images.type'
))
->joins(array(
'type' => 'left',
'table' => 'images',
'on' => 'images.user_id = users.id'
))
->SelectUsers( );
Above will produce the query string
Select users.*, images.url, images.type from users left join images on images.user_id = users.id
Group
$db
->group('images.type')
->columns(array(
'users.*',
'images.url',
'images.type'
))
->joins(array(
'type' => 'left',
'table' => 'images',
'on' => 'images.user_id = users.id'
))
->SelectUsers( );
Above will produce the query string
Select users.*, images.url, images.type from users left join images on images.user_id = users.id group by images.type
Order
$db
->order('images.created desc')
->group('images.type')
->columns(array(
'users.*',
'images.url',
'images.type'
))
->joins(array(
'type' => 'left',
'table' => 'images',
'on' => 'images.user_id = users.id'
))
->SelectUsers( );
Above will produce the query string
Select users.*, images.url, images.type from users left join images on images.user_id = users.id order by images.created group by images.type
Limit
$db
->limit(5)
->order('images.created desc')
->group('images.type')
->columns(array(
'users.*',
'images.url',
'images.type'
))
->joins(array(
'type' => 'left',
'table' => 'images',
'on' => 'images.user_id = users.id'
))
->SelectUsers();
Above will produce the query string
Select users.*, images.url, images.type from users left join images on images.user_id = users.id order by images.created group by images.type limit 5
Paging
$page = 1; // will get page 1
$db
->limit( 5 , $page )
->order('images.created desc')
->group('images.type')
->columns(array(
'users.*',
'images.url',
'images.type'
))
->joins(array(
'type' => 'left',
'table' => 'images',
'on' => 'images.user_id = users.id'
))
->SelectUsers( );
You can get the pagination information after doing like this
$db->$pgntion
Or condition
$conditions = array(
'type' => 2,
'or' => array(
'name like' => '%John%',
'email like' => '%john@email.com%',
'id in' => '(1,3,5)'
)
);
$db->SelectUsers( $conditions );
Above will produce the query string
Select * from users where type = '2' and (name like '%John%' or email like '%john@email.com%' or id in (1,3,5))
<br /><br />
Side note
Some cases are not considered thats why you can use the normal prepared statements
Insert
$query = "Insert into users ( name , email , type ) values ( :name , :email , :type )";
$data = array(
'name' => 'John Doe',
'email' => 'john@doe.com',
'type' => 2
);
$db->insertRow( $query , $data );
Select
$query = "Select * users where id = :id";
$data = array(
'id' => 1
);
$db->getRow( $query , $data ); // for single result
$db->getRows( $query , $data ); // for multiple results
Update
$query = "Update users set name = :name where type = :type";
$data = array(
'name' => 'John Doe',
'type' => 2
);
$db->updateRow( $query , $data );
Delete
$query = "Delete from users where type = :type";
$data = array(
'type' => 2
);
$db->deleteRow( $query , $data );
Related Skills
node-connect
347.2kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
108.0kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
347.2kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
347.2kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
