PHP Classes

Database class extending PDO: Execute queries composed programatically using PDO

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 67%Total: 1,636 All time: 2,409 This week: 39Up
Version License PHP version Categories
db-pdo 2.0.10GNU General Publi...5.6PHP 5, Databases
Description 

Author

This class can be used to execute queries composed programatically using PDO.

It can connect to a given database using PDO and execute queries composed programmatically.

The class can compose SQL SELECT, INSERT, UPDATE and DELETE queries with parameters passed to functions to define query fields, field values, order fields, group fields, having clause, join fields, and results limit values.

Picture of Jelle Sebreghts
  Performance   Level  
Innovation award
Innovation award
Nominee: 4x

 

Example

<?php

require_once '../vendor/autoload.php';
//change these to your database settings
define("DB_HOST", "localhost");
define("DB_NAME", "db_example");
define("DB_USER", "root");
define("DB_PASSWORD", "root");
$db = new Jelle_S\DataBase\Connection("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME, DB_USER, DB_PASSWORD);

$menu = $db->select('menu')->fields(array('label', 'page'))->orderby('weight')->run()->fetchAllAssoc();

print
"Initial data: <br /><pre>";
print_r($menu);
print
"</pre>";

$fields = array(
 
'label' => 'This is the menu label.',
 
'page' => 'home', //e.g relative link to the page'
 
'weight' => 6,
);
$db->insert('menu')->fields($fields)->run();
$last_insert_id = $db->lastInsertId();

$menu = $db->select('menu')->fields(array('label', 'page'))->orderby('weight')->run()->fetchAllAssoc();

print
"Data after insert: <br /><pre>";
print_r($menu);
print
"</pre>";

$updatefields = array(
 
'label' => 'This is the new label.',
 
'page' => 'contact',
 
'weight' => 5
);

$db->update('menu')->fields($updatefields)->where('id', $last_insert_id)->run();

$menu = $db->select('menu')->fields(array('label', 'page'))->orderby('weight')->run()->fetchAllAssoc();

print
"Data after update: <br /><pre>";
print_r($menu);
print
"</pre>";

$db->delete('menu')->where('id', $last_insert_id)->run();

$menu = $db->select('menu')->fields(array('label', 'page'))->orderby('weight')->run()->fetchAllAssoc();

print
"Data after delete: <br /><pre>";
print_r($menu);
print
"</pre>";



Details

db-pdo ======

  Files folder image Files (22)  
File Role Description
Files folder imageexamples (2 files)
Files folder imagesrc (1 file, 2 directories)
Accessible without login Plain text file .codeclimate.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:1,636
This week:0
All time:2,409
This week:39Up
User Ratings User Comments (2)
 All time
Utility:85%StarStarStarStarStar
Consistency:85%StarStarStarStarStar
Documentation:75%StarStarStarStar
Examples:85%StarStarStarStarStar
Tests:-
Videos:-
Overall:67%StarStarStarStar
Rank:474
 
good job
13 years ago (Ali M.Mirzaee)
70%StarStarStarStar
Excellent work, thank you.
13 years ago (Pali)
70%StarStarStarStar