lincanbin/php-pdo-mysql-class

一个类似 Python MySQLdb 的 PHP MySQL PDO 类,支持在“WHERE IN”语句中使用迭代器和参数绑定。

v2.2 2020-11-27 02:02 UTC

This package is auto-updated.

Last update: 2024-08-27 10:08:14 UTC


README

一个类似 Python MySQLdb 的 PHP MySQL PDO 类,支持在“WHERE IN”语句中使用迭代器和参数绑定。

安装

src/ 下的文件复制到您的程序中

或者

composer require lincanbin/php-pdo-mysql-class

初始化

<?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);
?>

防止 SQL 注入攻击

安全性:使用参数绑定方法

安全性示例

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

不安全:分割联合 SQL 字符串

不安全示例

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

基本用法

表 "fruit"

使用绑定获取数据(防 SQL 注入)

<?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'));
?>

结果

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

WHERE IN(需要命名占位符)

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

结果

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);
?>

结果

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

获取列

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

结果

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

获取行

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

结果

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

获取单个

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

结果

green

删除 / 更新 / 插入

这些操作将返回受影响的行数。(整数)

<?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
?>

获取最后插入 ID

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

获取自对象初始化以来的查询数

<?php
$DB->querycount;
?>

关闭连接

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

事务

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

迭代器

当您想要从数据库中读取大量数据用于统计分析或更新 Elastic Search 或 Solr 索引时,请使用迭代器。

迭代器 是一个可遍历的对象,它不会将查询的所有数据从 MySQL 读取到内存中。

因此,您可以使用 foreach 语句安全地处理数百万个 MySQL 结果集,而不用担心过度使用内存。

示例

$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);

返回

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