项目作者: lincanbin

项目描述 :
A PHP MySQL PDO class similar to the the Python MySQLdb, which supports iterator and parameter binding when using "WHERE IN" statement.
高级语言: PHP
项目地址: git://github.com/lincanbin/PHP-PDO-MySQL-Class.git
创建时间: 2014-11-09T15:40:02Z
项目社区:https://github.com/lincanbin/PHP-PDO-MySQL-Class

开源协议:Apache License 2.0

下载


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

  1. composer require lincanbin/php-pdo-mysql-class

Initialize

  1. <?php
  2. define('DBHost', '127.0.0.1');
  3. define('DBPort', 3306);
  4. define('DBName', 'Database');
  5. define('DBUser', 'root');
  6. define('DBPassword', '');
  7. require(__DIR__ . "/src/PDO.class.php");
  8. $DB = new Db(DBHost, DBPort, DBName, DBUser, DBPassword);
  9. ?>

Preventing SQL Injection Attacks

Safety: Use parameter binding method

Safety Example:

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

Unsafety: Split joint SQL string

Unsafety Example:

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

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):

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

Result:

  1. Array
  2. (
  3. [0] => Array
  4. (
  5. [id] => 1
  6. [name] => apple
  7. [color] => red
  8. )
  9. )

WHERE IN (needs named placeholder):

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

Result:

  1. Array
  2. (
  3. [0] => Array
  4. (
  5. [id] => 1
  6. [name] => apple
  7. [color] => red
  8. )
  9. [1] => Array
  10. (
  11. [id] => 2
  12. [name] => banana
  13. [color] => yellow
  14. )
  15. )
  1. <?php
  2. $query = "SELECT * FROM fruit WHERE name IN (:fruits) AND color = :color";
  3. // use multidimensional array as $params
  4. $params = array(
  5. "color" => "red",
  6. "fruits" => array(
  7. "apple",
  8. "banana"
  9. )
  10. );
  11. $DB->query($query, $params);
  12. ?>

Result:

  1. Array
  2. (
  3. [0] => Array
  4. (
  5. [id] => 1
  6. [name] => apple
  7. [color] => red
  8. )
  9. )

Fetching Column:

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

Result:

  1. Array
  2. (
  3. [0] => red
  4. [1] => yellow
  5. [2] => green
  6. )

Fetching Row:

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

Result:

  1. Array
  2. (
  3. [id] => 1
  4. [name] => apple
  5. [color] => red
  6. )

Fetching single:

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

Result:

  1. green

Delete / Update / Insert

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

  1. <?php
  2. // Delete
  3. $DB->query("DELETE FROM fruit WHERE id = :id", array("id"=>"1"));
  4. $DB->query("DELETE FROM fruit WHERE id = ?", array("1"));
  5. // Update
  6. $DB->query("UPDATE fruit SET color = :color WHERE name = :name", array("name"=>"strawberry","color"=>"yellow"));
  7. $DB->query("UPDATE fruit SET color = ? WHERE name = ?", array("yellow","strawberry"));
  8. // Insert
  9. $DB->query("INSERT INTO fruit(id,name,color) VALUES(?,?,?)", array(null,"mango","yellow"));//Parameters must be ordered
  10. $DB->query("INSERT INTO fruit(id,name,color) VALUES(:id,:name,:color)", array("color"=>"yellow","name"=>"mango","id"=>null));//Parameters order free
  11. ?>

Get Last Insert ID

  1. <?php
  2. $DB->lastInsertId();
  3. ?>

Get the number of queries since the object initialization

  1. <?php
  2. $DB->querycount;
  3. ?>

Close Connection

  1. <?php
  2. $DB->closeConnection();
  3. ?>

Transaction

  1. <?php
  2. try {
  3. $DB->beginTransaction();
  4. var_dump($DB->inTransaction()); // print "true"
  5. $DB->commit();
  6. } catch(Exception $ex) {
  7. // handle Error
  8. $DB->rollBack();
  9. }
  10. ?>

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:

  1. $iteratorInstance = $DB->iterator("SELECT * FROM fruit limit 0, 1000000;");
  2. $colorCountMap = array(
  3. 'red' => 0,
  4. 'yellow' => 0,
  5. 'green' => 0
  6. );
  7. foreach($iteratorInstance as $key => $value) {
  8. sendDataToElasticSearch($key, $value);
  9. $colorCountMap[$value['color']]++;
  10. }
  11. var_export($colorCountMap);

Return:

  1. array(3) {
  2. [red] => 2
  3. [yellow] => 2
  4. [green] => 1
  5. }