PHP Classes

Nemiro Data PHP: Compose and execute MySQL or PostgreSQL queries

Recommend this page to a friend!
  Info   View files Example   View files View files (24)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 181 All time: 8,702 This week: 206Up
Version License PHP version Categories
nemiro-data-php 2.0Custom (specified...5.3PHP 5, Databases
Description 

Author

This class can compose and execute MySQL or PostgreSQL queries.

It provides a common interface to access MySQL using MySQLi or PostgreSQL databases.

The common interface can execute queries with prepare statements or not, returning query results in arrays, etc..

Picture of Aleksey Nemiro
  Performance   Level  
Name: Aleksey Nemiro <contact>
Classes: 6 packages by
Country: Russian Federation Russian Federation
Age: 40
All time rank: 201458 in Russian Federation Russian Federation
Week rank: 86 Up10 in Russian Federation Russian Federation Up
Innovation award
Innovation award
Nominee: 3x

Winner: 1x

Example

<?php

# include config file
require_once 'config.php';

# include the db client classes file (use own path of the file location)
require_once '../Import.php';

# import MySql client
use Nemiro\Data\MySql as MySql;
# import command class
use Nemiro\Data\DBCommand as DBCommand;
# import parameters type list
use Nemiro\Data\DBParameterType as DBParameterType;

# best practice is to use 'try { } catch { }' blocks
try
{
   
# create client instance
   
$client = new MySql();

   
# you can specify the text of the query into the Command property
   
$client->Command = 'SELECT * FROM users';

   
# and execute the query by any method:
    # $client->GetRow(); # return single row
    # $client->GetTable(); # return all rows
    # $client->GetData(); # return array tables

    # for example, get all rows
   
$table = $client->GetTable();
   
var_dump($table);

   
# you can build a query from the input parameters,
    # but you will need to check the type and value of incoming data
    # it is bad practice
   
$client->Command = 'DELETE FROM users WHERE id_users = '.(int)$_GET['id'];

   
# it is best to use parameterized queries
   
$client->Command = new DBCommand('DELETE FROM users WHERE id_users = @id_users');
   
$client->Command->Parameters->Add('@id_users', $_GET['id'], DBParameterType::Integer);
   
# or
    # $client->Command->Parameters->Add('@id_users', $_GET['id']);
    # or
    # $client->Command->Parameters->Add('@id_users').SetValue($_GET['id']);
    # it is a safe solution

    # execute the query
   
$affectedRows = $client->ExecuteNonQuery();

    echo
'Deleted: '.$affectedRows.'<br />';
}
catch (
Exception $ex)
{
    echo
'Error: '.$ex->getMessage();
}
?>


Details

Nemiro.Data.PHP Latest Stable Version Total Downloads License

Nemiro.Data.PHP is a small set of utility classes for working with databases MySql and PostgreSQL.

To work with the databases used five simple methods: ExecuteNonQuery, ExecuteScalar, GetData, GetTable and GetRow.

The classes allow you to use parameterized queries, which makes working with databases secure.

Nemiro.Data.PHP is licensed under the Apache License Version 2.0.

Features

  • Client for MySql;
  • Client for PostgreSQL;
  • A single interface to work with different data providers;
  • Automatic control of database connections;
  • Parameterized queries.

System Requirements

  • PHP 5 >= 5.3;
  • MySQL >= 5.6;
  • PostgreSQL >= 7.4.

NOTE: Working with the earlier versions just has not been tested.

Supports

Further support and development of the project is not planned. Welcome to .NET ;-)

How to use the project?

The files of the project are made in Visual Studio 2013 with the extension PHP Tools for Visual Studio.

To use the classes in your own projects, it is recommended to put all the solution files in a folder \Nemiro\Data (corresponds to the namespace).

How to use the classes?

Configuration

By default, classes use the database connection settings of the following constants:

// MySql
define('MYSQL_DB_NAME', '%your database name here%');
define('MYSQL_DB_USER', '%your database username here%');
define('MYSQL_DB_PASSWORD', '%your database password here%');
define('MYSQL_DB_HOST', 'localhost');
define('MYSQL_DB_PORT', 3306);
define('MYSQL_DB_MODE', 2);

// PostgreSQL
define('PGSQL_DB_NAME', '%your database name here%');
define('PGSQL_DB_USER', '%your database username here%');
define('PGSQL_DB_PASSWORD', '%your database password here%');
define('PGSQL_DB_HOST', 'localhost');
define('PGSQL_DB_PORT', 5432);
define('PGSQL_DB_MODE', 1);

The DB_MODE may be one of the following:

  • 0 - manual;
  • 1 - auto - open and close for each request;
  • 2 - smart (recomended).

You can use individual connection settings, which must be specified when you create an instance of a database client.

Including files

To use the database clients, you must include the following files:

require_once './Nemiro/Data/Import.php';

or

require_once './Nemiro/Data/MySql.php';
require_once './Nemiro/Data/PgSql.php';

Importing namespaces

For convenience, you can import the necessary classes in your code:

// client for MySql
use Nemiro\Data\MySql as MySql;
// client for PostgreSQL
use Nemiro\Data\PgSql as PgSql;
// query builder
use Nemiro\Data\DBCommand as DBCommand;

Examples of use

The following example creates a simple query to select all records from the table [messages].

Records obtained by the GetTable method, which returns an array of rows.

// create client instance for MySql
$client = new MySql();

// create a new command
$client->Command = new DBCommand('SELECT * FROM messages');

// get table
$table = $client->GetTable();

// output the table rows
echo '<pre>';
foreach($table as $row)
{
	print_r($row);
}
echo '</pre>';

The following example creates a parameterized query to add records to the table [users].

The query is executed by the ExecuteScalar method, which returns the ID of added record.

// create client instance for MySql
$client = new MySql();

// create a new command
$client->Command = new DBCommand
(
	'INSERT INTO users (username, date_created) '.
	'VALUES (@username, @date_created)'
);

// @username and @date_created is parameters name, 
// add a values for this parameters
$client->Command->Parameters->Add('@date_created')->SetValue(date('Y-m-d H-i-s'));
$client->Command->Parameters->Add('@username')->SetValue('anyname');

// execute the command
$newId = $client->ExecuteScalar();

echo 'ID = '.$newId;

The following example creates multiple queries and executed by the GetData method, which returns an array of tables.

// create client instance for MySql
$client = new MySql();

// create commands
$firtCommand = new DBCommand('SELECT * FROM users WHERE is_blocked = 0');

$secondCommand = new DBCommand
(
	'SELECT * FROM messages WHERE id_users IN '.
	'(SELECT id_users FROM users WHERE is_blocked = 0) AND '.
	'subject LIKE @search_subject'
);
$secondCommand->Parameters->Add('@search_subject', '%hello%');

$thirdCommand = 'SELECT * FROM files';

// etc...

// add commands to client
$client->Command = array($firtCommand, $secondCommand, $thirdCommand);

// and execute all command
$data = $client->GetData();

// output results

echo '<pre>';
foreach ($data as $table)
{
	print_r($table);
}
echo '</pre>';

  Files folder image Files  
File Role Description
Files folder imagesamples (10 files)
Plain text file ConnectionMode.php Class Class source
Plain text file DBCommand.php Class Class source
Plain text file DBParameter.php Class Class source
Plain text file DBParameterCollection.php Class Class source
Plain text file DBParameterType.php Class Class source
Plain text file IDBClient.php Class Class source
Plain text file Import.php Class Class source
Accessible without login Plain text file LICENSE Lic. License text
Plain text file MySql.php Class Class source
Plain text file PGException.php Class Class source
Plain text file PgSql.php Class Class source
Plain text file PgStmt.php Class Class source
Accessible without login Plain text file README.md Doc. Documentation
Plain text file TDBClient.php Class Class source

  Files folder image Files  /  samples  
File Role Description
  Accessible without login Plain text file config.php Conf. Example script
  Accessible without login Plain text file index.php Aux. Example script
  Accessible without login Plain text file mysql_getdata.php Example Example script
  Accessible without login Plain text file mysql_getrow.php Example Example script
  Accessible without login Plain text file mysql_gettable.php Example Example script
  Accessible without login Plain text file mysql_insert_get_id.php Example Example script
  Accessible without login Plain text file mysql_simply.php Example Example script
  Accessible without login Plain text file pgsql_simply.php Example Example script
  Accessible without login Plain text file pg_connstr.php Example Example script
  Accessible without login Plain text file pg_insert_get_id.php Example Example script

 Version Control Unique User Downloads Download Rankings  
 100%
Total:181
This week:0
All time:8,702
This week:206Up