fumagally/php-pdo-mysql
Composer 安装命令:
composer require fumagally/php-pdo-mysql
包简介
A PHP MySQL PDO Class
README 文档
README
A PHP MySQL PDO class.
Install
Copy the files under src/ to your program
OR
composer require fumagally/php-pdo-mysql
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 }
fumagally/php-pdo-mysql 适用场景与选型建议
fumagally/php-pdo-mysql 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 06 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「iterator」 「mysql」 「pdo」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 fumagally/php-pdo-mysql 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 fumagally/php-pdo-mysql 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 fumagally/php-pdo-mysql 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
collection php library
Store your language lines in the database, yaml or other sources
PHP PDO Mysql select statement iterator implemented as multiple queries using LIMIT clauses
Iteration tools for PHP
Sequential way to run Symfony Processes in parallel
统计信息
- 总下载量: 20
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2022-06-01