All Projects → lincanbin → Php Pdo Mysql Class

lincanbin / Php Pdo Mysql Class

Licence: apache-2.0
A PHP MySQL PDO class similar to the the Python MySQLdb, which supports iterator and parameter binding when using "WHERE IN" statement.

Projects that are alternatives of or similar to Php Pdo Mysql Class

Easydb
Easy-to-use PDO wrapper for PHP projects.
Stars: ✭ 624 (+192.96%)
Mutual labels:  pdo, mysql
Dtool
数据生成器,数据库工具,数据库填充,伪数据,faker,mysql数据字典,数据库比对
Stars: ✭ 28 (-86.85%)
Mutual labels:  pdo, mysql
Leafpub
Simple, beautiful, open source publishing.
Stars: ✭ 645 (+202.82%)
Mutual labels:  pdo, mysql
Php Sql Query Builder
An elegant lightweight and efficient SQL Query Builder with fluid interface SQL syntax supporting bindings and complicated query generation.
Stars: ✭ 313 (+46.95%)
Mutual labels:  pdo, mysql
Nukeviet
NukeViet CMS is multi Content Management System. NukeViet CMS is the 1st open source content management system in Vietnam. NukeViet was awarded the Vietnam Talent 2011, the Ministry of Education and Training Vietnam officially encouraged to use.
Stars: ✭ 113 (-46.95%)
Mutual labels:  pdo, mysql
Dibi
Dibi - smart database abstraction layer
Stars: ✭ 373 (+75.12%)
Mutual labels:  pdo, mysql
Ezsql
PHP class to make interacting with a database ridiculusly easy
Stars: ✭ 804 (+277.46%)
Mutual labels:  pdo, mysql
Php Mysql Class
Simple MySQL class written in PHP, for interfacing with a MySQL database.
Stars: ✭ 349 (+63.85%)
Mutual labels:  pdo, mysql
Swpdo
Swoole Coroutine SQL component like PDO | 0成本迁移PDO到Swoole高性能协程客户端
Stars: ✭ 64 (-69.95%)
Mutual labels:  pdo, mysql
Php frameworks analysis
php框架源码分析
Stars: ✭ 57 (-73.24%)
Mutual labels:  pdo, mysql
Fluentpdo
A PHP SQL query builder using PDO
Stars: ✭ 783 (+267.61%)
Mutual labels:  pdo, mysql
Pdo
Connecting to MySQL in PHP using PDO.
Stars: ✭ 187 (-12.21%)
Mutual labels:  pdo, mysql
Mysqldump Php
PHP version of mysqldump cli that comes with MySQL
Stars: ✭ 975 (+357.75%)
Mutual labels:  pdo, mysql
Basicdb
PDO ile geliştirilmiş kullanımı kolay veritabanı sınıfıdır.
Stars: ✭ 127 (-40.38%)
Mutual labels:  pdo, mysql
Hydrahon
🐉 Fast & standalone PHP MySQL Query Builder library.
Stars: ✭ 194 (-8.92%)
Mutual labels:  pdo, mysql
Nodejs
Node.js基础与应用教程,适合初学者入门,以及有一定经验的开发者提高。Node.js全栈交流QQ群:423652352,node.js或者全栈开发培训QQ群:579500717
Stars: ✭ 202 (-5.16%)
Mutual labels:  mysql
Dbtool
数据库工具,根据表结构文档生成创建表sql,根据数据库表信息导出Model和表结构文档,根据文档生成数据库表,根据已有Model文件生成创建数据库表sql
Stars: ✭ 206 (-3.29%)
Mutual labels:  mysql
Synch
Sync data from the other DB to ClickHouse(cluster)
Stars: ✭ 200 (-6.1%)
Mutual labels:  mysql
Learningsummary
涵盖大部分Java进阶需要掌握的知识,包括【微服务】【中间件】【缓存】【数据库优化】【搜索引擎】【分布式】等等,欢迎Star~
Stars: ✭ 201 (-5.63%)
Mutual labels:  mysql
Blog
Hi, I am CrazyCodes, and here are all my articles
Stars: ✭ 212 (-0.47%)
Mutual labels:  mysql

PHP-PDO-MySQL-Class Build Status

A PHP MySQL PDO class similar to the Python MySQLdb, which supports iterator and parameter binding when using "WHERE IN" statement.

Install

Copy the files under src/ to your program

OR

composer require lincanbin/php-pdo-mysql-class

Initialize

<?php
define('DBHost', '127.0.0.1');
define('DBPort', 3306);
define('DBName', 'Database');
define('DBUser', 'root');
define('DBPassword', '');
require(__DIR__ . "/src/PDO.class.php");
$DB = new Db(DBHost, DBPort, DBName, DBUser, DBPassword);
?>

Preventing SQL Injection Attacks

Safety: Use parameter binding method

Safety Example:

<?php
$DB->query("SELECT * FROM fruit WHERE name=?", array($_GET['name']));
?>

Unsafety: Split joint SQL string

Unsafety Example:

<?php
$DB->query("SELECT * FROM fruit WHERE name=".$_GET['name']);
?>

Basic Usage

table "fruit"

id name color
1 apple red
2 banana yellow
3 watermelon green
4 pear yellow
5 strawberry red

Fetching with Bindings (ANTI-SQL-INJECTION):

<?php
$DB->query("SELECT * FROM fruit WHERE name=? and color=?",array('apple','red'));
$DB->query("SELECT * FROM fruit WHERE name=:name and color=:color",array('name'=>'apple','color'=>'red'));
?>

Result:

Array
(
	[0] => Array
		(
			[id] => 1
			[name] => apple
			[color] => red
		)
)

WHERE IN (needs named placeholder):

<?php
$DB->query("SELECT * FROM fruit WHERE name IN (:fruits)",array(array('apple','banana')));
?>

Result:

Array
(
	[0] => Array
		(
			[id] => 1
			[name] => apple
			[color] => red
		)
	[1] => Array
		(
			[id] => 2
			[name] => banana
			[color] => yellow
		)
)
<?php
$query = "SELECT * FROM fruit WHERE name IN (:fruits) AND color = :color";
// use multidimensional array as $params
$params = array(
	"color" => "red",
	"fruits" => array(
		"apple",
		"banana"
	)
);
$DB->query($query, $params);
?>

Result:

Array
(
	[0] => Array
		(
			[id] => 1
			[name] => apple
			[color] => red
		)
)

Fetching Column:

<?php
$DB->column("SELECT color FROM fruit WHERE name IN (:color)",array('apple','banana','watermelon'));
?>

Result:

Array
(
	[0] => red
	[1] => yellow
	[2] => green
)

Fetching Row:

<?php
$DB->row("SELECT * FROM fruit WHERE name=? and color=?",array('apple','red'));
?>

Result:

Array
(
	[id] => 1
	[name] => apple
	[color] => red
)

Fetching single:

<?php
$DB->single("SELECT color FROM fruit WHERE name=? ",array('watermelon'));
?>

Result:

green

Delete / Update / Insert

These operations will return the number of affected result set. (integer)

<?php
// Delete
$DB->query("DELETE FROM fruit WHERE id = :id", array("id"=>"1"));
$DB->query("DELETE FROM fruit WHERE id = ?", array("1"));
// Update
$DB->query("UPDATE fruit SET color = :color WHERE name = :name", array("name"=>"strawberry","color"=>"yellow"));
$DB->query("UPDATE fruit SET color = ? WHERE name = ?", array("yellow","strawberry"));
// Insert
$DB->query("INSERT INTO fruit(id,name,color) VALUES(?,?,?)", array(null,"mango","yellow"));//Parameters must be ordered
$DB->query("INSERT INTO fruit(id,name,color) VALUES(:id,:name,:color)", array("color"=>"yellow","name"=>"mango","id"=>null));//Parameters order free
?>

Get Last Insert ID

<?php
$DB->lastInsertId();
?>

Get the number of queries since the object initialization

<?php
$DB->querycount;
?>

Close Connection

<?php
$DB->closeConnection();
?>

Transaction

<?php
try {
    $DB->beginTransaction();
    var_dump($DB->inTransaction()); // print "true"
    $DB->commit();
} catch(Exception $ex) {
    // handle Error
    $DB->rollBack();
}
?>

Iterator

Use iterator when you want to read thousands of data from the database for statistical or full update of Elastic Search or Solr indexes.

Iterator is a traversable object that does not read all the data queried from MySQL into memory.

So you can safely use foreach to handle millions of MySQL result sets without worrying about excessive memory usage.

Example:

$iteratorInstance = $DB->iterator("SELECT * FROM fruit limit 0, 1000000;");
$colorCountMap = array(
    'red' => 0,
    'yellow' => 0,
    'green' => 0
);
foreach($iteratorInstance as $key => $value) {
    sendDataToElasticSearch($key, $value);
    $colorCountMap[$value['color']]++;
}
var_export($colorCountMap);

Return:

array(3) {
  [red] => 2
  [yellow] => 2
  [green] => 1
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].