Connection PDO: Execute common SQL queries using PDO driver class

Recommend this page to a friend!
  Info   View files View files (7)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 291 All time: 7,429 This week: 332Up
Version License PHP version Categories
connection-pdo 1.0.4MIT/X Consortium ...5.6PHP 5, Databases
Description Author

This package package execute common SQL queries using PDO driver class.

It can connect to a database and uses driver classes to execute common queries that can be adapted for different databases.

The driver classes execute prepared queries for SELECT, INSERT, UPDATE, DELETE, DROP and CREATE TABLE and SHOW TABLES.

The comments in the code are in Portuguse. In Portuguese:

Esta classe se conecta ao seu banco de dados via PDO. Contendo alguns métodos para simplificar as operações.

Picture of Carlos Eduardo Barcelos Amaral
  Performance   Level  
Name: Carlos Eduardo Barcelos ... <contact>
Classes: 3 packages by
Country: Brazil Brazil
Age: 30
All time rank: 2181145 in Brazil Brazil
Week rank: 496 Up30 in Brazil Brazil Up

Details

ConnectionPDO

ConnectionPDO é uma classe PHP para realizar a gestão do banco de dados de uma forma mais prática.

Version

1.1.0

Methods

- Drop - Create - Insert - Update - Delete - Select - ExecuteSQL (generic query) - Transaction (Begin, Rollback, Commit)

Exemplo

Conexão

$host = 'localhost';
$user = 'root';
$pass = '';
$base = 'test';

$con = new ConnectionPDO($host,$user,$pass,$base);

DROP

$con->drop('tab_teste');

CREATE

$fields = Array(
      'id' => Array(
         'type' => 'int',
         'size' => '4',
         'comment' => 'first key'
      ),
      'name' => Array(
         'type' => 'varchar',
         'size' => '60',
         'comment' => 'test name'
      ),
      'col3' => Array(
         'type' => 'varchar',
         'size' => '60',
         'default' => NULL,
         'comment' => 'test name'
      )
   );
   $con->create('tab_teste',$fields,'id','InnoDB',false);

O quinto parâmetro é para eliminar a tabela, se existir.

INSERT

$data = Array('id'=>1,'name' => 'First Record', 'col3' => 'test ');
$con->insert('tab_teste',$data);

$data = Array('id'=>2,'name' => 'Second Record', 'col3' => 'test ');
$con->insert('tab_teste',$data);

$data = Array('id'=>3,'name' => 'Third Record', 'col3' => 'test ');
$con->insert('tab_teste',$data);

DELETE

$where = Array('id'=>1);
$con->delete('tab_teste', $where);

UPDATE

$data = Array(
   'name' => 'Now this is the first record', 
   'col3' => 'First record'
);
$where = Array('id'=>2);
$con->update('tab_teste',$data, $where);

SELECT

$where = Array('id' => Array('BETWEEN'=>Array(2,3)));
$res = $con->select('tab_teste',$where);
$tab = $res->fetch(PDO::FETCH_ASSOC);

Resultado do select:

------------------------------------------------------
| id | name                           | col3         |
------------------------------------------------------
| 2  | Now this is the first record   | First record |
------------------------------------------------------
| 3  | Third Record                   | test         |
------------------------------------------------------

WHERE

Alguns exemplos do parâmetro $where:

$where = 'id = 1'; 
// Resultado: id = 1

$where = Array('id' => 1); 
// Resultado: id = 1

$where = Array('id' => 1,'$OR1'=>'OR','col3' => 'test'); 
// Resultado: id = 1 OR col3 = 'test'

$where - Array('col3' => array('LIKE' => 'recor'))
// Resultado: col3 LIKE '%recor%'

$where = Array('id' => Array(1,'>>>',10, Array(3,6,8))); 
// Resultado: id IN (1,2,4,5,7,9,10)

$where = Array('id' => Array('BETWEEN' => Array(1,10)));
// Resultado:  id BETWEEN 1 AND 10

$where = Array('id' => Array('NOT' => Array(1,2,3,12,45)));
// Resultado: id NOT IN (1,2,3,12,45)

$where = Array('id' => Array('NOT' => Array(1,'>>>',10, Array(3,6,8)))); 
// Resultado: id NOT IN (1,2,4,5,7,9,10)


$where = Array(
  'id' => array('NOT' => array(1,'>>>',6,array(3,5))),
  '$OR'=>'OR',
  'col3' => array('LIKE' => 'recor')
);
// Resultado: id NOT IN (1, 2, 4, 6) OR col3 LIKE '%recor%'

Para inserir algo entre colunas basta passar um parâmetro iniciando com $ seguido de qualquer _string_:


$where = Array('id'=>3, '$a'=>'OR', 'name'=>Array('LIKE'=>'first'))

Também pode passar operadores como >, >=, <=, <, para coluna com valores numéricos. Basta dar um espaço do nome da coluna 'coluna >' => 'valor':

$where = Array('id >'=>1)
// Resultado: id > 1

Outro exemplo:

$where = Array(
   'id >' => 5,
   '$1'=>'OR',
   '$2'=>'(',
   'col3' => array('LIKE' => 'recor'),
   '$3'=>'OR',
   'name' => array('LIKE' => 'Recor'),
   '$4'=>')'
);
// Resultado: `id` > 5 OR  ( `col3` LIKE '%recor%' OR `name` LIKE '%Recor%' )

Licença

Distribuição sob licença MIT

Projeto

Author: Kaduamaral

Author Blog: Devcia

  Files folder image Files  
File Role Description
Files folder imagedrivers (2 files)
Plain text file composer.json Data Auxiliary data
Plain text file ConnectionPDO.php Class Class source
Plain text file LICENSE Lic. License
Plain text file README.md Data Auxiliary data
Plain text file teste.php Test Unit test script

  Files folder image Files  /  drivers  
File Role Description
  Plain text file driverinterface.php Class Driver interface
  Plain text file sqldriver.mysql.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:291
This week:0
All time:7,429
This week:332Up
For more information send a message to info at phpclasses dot org.