Recommend this page to a friend! |
Download .zip |
Info | Example | View files (24) | Download .zip | Reputation | Support forum | Blog | Links |
Last Updated | Ratings | Unique User Downloads | Download Rankings | |||||
2015-11-05 (11 months ago) | Not yet rated by the users | Total: 164 | All time: 8,087 This week: 1,081 |
Version | License | PHP version | Categories | |||
nemiro-data-php 2.0 | Custom (specified... | 5.3 | PHP 5, Databases |
Description | Author | |
This class can compose and execute MySQL or PostgreSQL queries. |
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.
NOTE: Working with the earlier versions just has not been tested.
Further support and development of the project is not planned. Welcome to .NET ;-)
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).
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:
You can use individual connection settings, which must be specified when you create an instance of a database client.
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';
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;
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 |
File | Role | Description | ||
---|---|---|---|---|
samples (10 files) | ||||
ConnectionMode.php | Class | Class source | ||
DBCommand.php | Class | Class source | ||
DBParameter.php | Class | Class source | ||
DBParameterCollection.php | Class | Class source | ||
DBParameterType.php | Class | Class source | ||
IDBClient.php | Class | Class source | ||
Import.php | Class | Class source | ||
LICENSE | Lic. | License text | ||
MySql.php | Class | Class source | ||
PGException.php | Class | Class source | ||
PgSql.php | Class | Class source | ||
PgStmt.php | Class | Class source | ||
README.md | Doc. | Documentation | ||
TDBClient.php | Class | Class source |
Files | / | samples |
File | Role | Description |
---|---|---|
config.php | Conf. | Example script |
index.php | Aux. | Example script |
mysql_getdata.php | Example | Example script |
mysql_getrow.php | Example | Example script |
mysql_gettable.php | Example | Example script |
mysql_insert_get_id.php | Example | Example script |
mysql_simply.php | Example | Example script |
pgsql_simply.php | Example | Example script |
pg_connstr.php | Example | Example script |
pg_insert_get_id.php | Example | Example script |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.