PHP Classes

Connection MSi: Access MySQL database using MySQLi functions

Recommend this page to a friend!
  Info   Example   Screenshots   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-01-09 (9 months ago) RSS 2.0 feedNot enough user ratingsTotal: 305 All time: 7,377 This week: 36Up
Version License PHP version Categories
connection-msi 1.0.13GNU General Publi...5PHP 5, Databases
Description 

Author

This class can access MySQL database using MySQLi functions.

It can connect to a given MySQL database server and perform several type of common operations using given parameters like executing SELECT, INSERT, UPDATE and DELETE queries, creating and dropping tables, and managing transactions.

In Portuguese:

Classe de conexão com MySQLi de uma forma mais prática, com métodos de

- CREATE
- DROP
- INSERT
- UPDATE
- DELETE
- SELECT
- ExecuteSQL (execução SQL genérico)
- TRANSACTION

Picture of Carlos Eduardo Barcelos Amaral
  Performance   Level  
Name: Carlos Eduardo Barcelos ... <contact>
Classes: 3 packages by
Country: Brazil Brazil
Age: 33
All time rank: 2161144 in Brazil Brazil
Week rank: 180 Up12 in Brazil Brazil Up

Example

<?php
error_reporting
(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);



function
__autoload($class){
  
$dir = './';
  
$ext = '.php';
   if (
file_exists($dir.$class.$ext)) require_once $dir.$class.$ext;
   else exit(
'Coul\'d open '.$class.'!');
}


$con = new ConnectionMSi('localhost','root','','test');
$con->DebugIn();

?><!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="utf-8">
   <title>ConnectionMSi Test</title>
   <style>
      body {
         margin: 0;
         padding: 0;
         background-color: #757575;
      }
      main {
         display: block;
         margin: 0 auto;
         width: 620px;
      }
      label {
         display: block;
         padding: 10px 20px;
         margin: 50px auto 0;
         background-color: #F0F0F0;
         color: #06C;
         font-family: Verdana, Arial, sans-serif;
         font-size: 24px;
         box-shadow: 2px 3px 7px rgba(0,0,0,0.7);
      }
      pre {
         display: block;
         margin: 0 auto 50px;
         padding: 20px;
         border-top: 1px solid #FFF;
         max-width: 100%;
         background-color: #F8F8F8;
         color:#333;
         white-space: pre-wrap; /* css-3 */
         white-space: -moz-pre-wrap; /* Mozilla, since 1999 */
         white-space: -pre-wrap; /* Opera 4-6 */
         white-space: -o-pre-wrap; /* Opera 7 */
         word-wrap: break-word; /* Internet Explorer 5.5+ */
         box-shadow: 2px 3px 7px rgba(0,0,0,0.7);
      }
      table {
         border-collapse: collapse;
         width: 100%;
      }
      th{
         font-weight: bold;
      }
      table, th, td {
          padding: 4px;
          border-color: #999;
      }
   </style>
</head>
<body>
<main>
   <?php

  
echo '<label>Drop Statement</label>';
   echo
'<pre>';
   echo
$con->Drop('tab_teste');
   echo
'</pre>';

  
$fields = Array(
     
'id' => Array(
        
'type' => 'int',
        
'size' => '4',
        
'comment' => 'first key',
        
'auto' => true
     
),
     
'name' => Array(
        
'type' => 'varchar',
        
'size' => '60',
        
'comment' => 'test name'
     
),
     
'col3' => Array(
        
'type' => 'varchar',
        
'size' => '60',
        
'default' => NULL,
        
'comment' => 'test name'
     
)
   );
   echo
'<label>Create Statement</label>';
   echo
'<pre>';
   echo
$con->Create('tab_teste',$fields,'id','InnoDB',false);
   echo
'</pre>';

   echo
'<label>Insert Statement</label>';
   echo
'<pre>';
   echo
$con->Insert('tab_teste',Array('id'=>1,'name' => 'First Record', 'col3' => 'test '));
   echo
PHP_EOL;
   echo
$con->Insert('tab_teste',Array('id'=>2,'name' => 'Second Record', 'col3' => 'test '));
   echo
PHP_EOL;
   echo
$con->Insert('tab_teste',Array('id'=>3,'name' => 'Third Record', 'col3' => 'test '));
   echo
PHP_EOL;
   echo
$con->Insert('tab_teste',Array('name' => 'Quarto', 'col3' => '4 '));
   echo
PHP_EOL;
   echo
$con->Insert('tab_teste',Array('name' => 'Quinto', 'col3' => '5 '));
   echo
PHP_EOL;
   echo
$con->Insert('tab_teste',Array('name' => 'Sexto', 'col3' => '6 '));
   echo
'</pre>';

   echo
'<label>Delete Statement</label>';
   echo
'<pre>';
   echo
$con->Delete('tab_teste', Array('id'=>1));
   echo
'</pre>';

   echo
'<label>Update Statement</label>';
   echo
'<pre>';
   echo
$con->Update('tab_teste',Array('name' => 'Now this is the first record', 'col3' => 'First record '), Array('id'=>2));
   echo
'</pre>';

  
$where = Array(
     
'id' => array('NOT' => array(1,'>>>',6,array(3,5))),
     
'OR',
     
'col3' => array('LIKE' => 'recor')
   );

   echo
'<label>INSERT Statement</label>';
   echo
'<pre>';
   echo
$con->Select('tab_teste',$where);
   echo
'</pre>';


  
$con->DebugOut();


   echo
'<label>Select Statement</label>';
  
$res = $con->Select('tab_teste',$where);
  
$tab = $res->fetch_all(MYSQLI_ASSOC);
   if (
is_array($tab)){
     
$_cols = Array();
      foreach (
$tab as $key => $row) {
         foreach (
$row as $col => $val) {
            if (
count($_cols) != count($row)) $_cols[] = $col;
            else break;
         }
         break;
      }
      echo
'<pre><table cellpadding="0" cellspacing="0" border="1"><thead><tr>';
      foreach (
$_cols as $colun) {
         echo
"<th>{$colun}</th>";
      }
      echo
'</tr></thead><tbody>';

      foreach (
$tab as $key => $row) {
         echo
'<tr>';
         foreach (
$row as $col => $val) {
            echo
"<td>{$val}</td>";
         }
         echo
'</tr>';
      }
      echo
'</tbody></table></pre>';
   } else {
      echo
'<pre>'.$con->_lastSql.'</pre>';
   }

  
?>
</main>
</body>
</html>


Details

ConnectionMSi

ConnectionMSi is a PHP class to carry out the management of the database in a more practical way.

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

Methods

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

Connection

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

$con = new ConnectionMSi($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);

The fifth parameter is to drop the table if it exists.

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

INSERT

$con->Begin();
$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);
$con->Commit();

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_all(MYSQLI_ASSOC);

Select Result for $tab

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

WHERE

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

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

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

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

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

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

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

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


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

Version

1.0.0

License

GPL v2

Project

Author: Kaduamaral

Devcia: ConnectionMSi Docs

GitHub: Project


Screenshots (3)  
  • ss3.png
  • ss2.png
  • ss1.png
  Files folder image Files (7)  
File Role Description
Accessible without login Plain text file README.md Doc. Readme
Accessible without login Plain text file example.php Example Exemplos
Plain text file ConnectionMSi.php Class ConnectionMSi Class
Accessible without login Plain text file LICENSE Data Create

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  
 57%
Total:305
This week:0
All time:7,377
This week:36Up