izap / mysql-pdo
MySQL pdo 驱动
Requires
- php: >= 5.3.0
This package is auto-updated.
Last update: 2024-09-06 13:35:48 UTC
README
PHP-MySQL 数据库类,使用 PDO 扩展。
如果您有任何问题,请访问: http://indieteq.com/index/readmore/how-to-prevent-sql-injection-in-php
使用类
1. 修改 settings.ini.php 中的数据库设置
注意:如果 PDO 加载速度慢,请将 localhost 改为 -> 127.0.0.1 !
[SQL]
host = 127.0.0.1
user = root
password =
dbname = yourdatabase
2. 在项目中引入该类
<?php require("Db.class.php");
3. 创建实例
<?php // The instance $db = new Db();
4. 日志 - 修改根目录的读写权限
每次数据库类抛出异常时,都会创建或修改一个日志文件。这些日志存储在日志目录中。这意味着数据库类需要写入日志目录的权限。如果文件位于 Web 服务器上,您必须修改根目录的权限,否则您将收到“权限被拒绝”错误。
日志文件是一个简单的纯文本文件,其文件名为当前日期(年-月-日)。
示例
以下是一些数据库类基本功能的示例。我已经包括了一个 SQL 导出文件,以便您可以轻松测试数据库类功能。
人员表
从表中获取所有内容
<?php // Fetch whole table $persons = $db->query("SELECT * FROM persons");
使用绑定获取(防止 SQL 注入)
绑定参数是防止 SQL 注入的最佳方式。该类准备 SQL 查询,然后绑定参数。
有三种不同的方式来绑定参数。
<?php // 1. Read friendly method $db->bind("id","1"); $db->bind("firstname","John"); $person = $db->query("SELECT * FROM Persons WHERE firstname = :firstname AND id = :id"); // 2. Bind more parameters $db->bindMore(array("firstname"=>"John","id"=>"1")); $person = $db->query("SELECT * FROM Persons WHERE firstname = :firstname AND id = :id")); // 3. Or just give the parameters to the method $person = $db->query("SELECT * FROM Persons WHERE firstname = :firstname",array("firstname"=>"John","id"=>"1"));
有关防止 SQL 注入的更多信息: http://indieteq.com/index/readmore/how-to-prevent-sql-injection-in-php
获取行
此方法始终只返回一行。
<?php // Fetch a row $ages = $db->row("SELECT * FROM Persons WHERE id = :id", array("id"=>"1"));
结果
获取单个值
此方法只返回记录的一个单个值。
<?php // Fetch one single value $db->bind("id","3"); $firstname = $db->single("SELECT firstname FROM Persons WHERE id = :id");
结果
获取列
<?php // Fetch a column $names = $db->column("SELECT Firstname FROM Persons");
结果
删除 / 更新 / 插入
当使用查询方法执行删除、更新或插入语句时,将返回受影响的行。
<?php // Delete $delete = $db->query("DELETE FROM Persons WHERE Id = :id", array("id"=>"1")); // Update $update = $db->query("UPDATE Persons SET firstname = :f WHERE Id = :id", array("f"=>"Jan","id"=>"32")); // Insert $insert = $db->query("INSERT INTO Persons(Firstname,Age) VALUES(:f,:age)", array("f"=>"Vivek","age"=>"20")); // Do something with the data if($insert > 0 ) { return 'Succesfully created a new person !'; }
方法参数
每个执行查询的方法都有名为 bindings 的可选参数。
row 和 query 方法有一个第三个可选参数,即获取样式。默认获取样式是 PDO::FETCH_ASSOC,它返回一个关联数组。
以下是一个示例
<?php // Fetch style as third parameter $person_num = $db->row("SELECT * FROM Persons WHERE id = :id", array("id"=>"1"), PDO::FETCH_NUM); print_r($person_num); // Array ( [0] => 1 [1] => Johny [2] => Doe [3] => M [4] => 19 )
有关 PDO 获取样式的更多信息: https://php.ac.cn/manual/en/pdostatement.fetch.php
EasyCRUD
easyCRUD 是一个类,您可以使用它轻松执行基本的 SQL 操作(如插入、更新、选择、删除)在您的数据库上。它使用我创建的数据库类来执行 SQL 查询。
实际上,它只是一个小的 ORM 类。
如何使用 easyCRUD
1. 首先,创建一个新类。然后引入 easyCRUD 类。
2. 将您的类扩展到基类 Crud,并在类中添加以下字段。
示例类
<?php require_once("easyCRUD.class.php"); class YourClass Extends Crud { # The table you want to perform the database actions on protected $table = 'persons'; # Primary Key of the table protected $pk = 'id'; }
EasyCRUD 应用示例。
创建新人员
<?php // First we"ll have create the instance of the class $person = new person(); // Create new person $person->Firstname = "Kona"; $person->Age = "20"; $person->Sex = "F"; $created = $person->Create(); // Or give the bindings to the constructor $person = new person(array("Firstname"=>"Kona","age"=>"20","sex"=>"F")); $created = $person->Create(); // SQL Equivalent "INSERT INTO persons (Firstname,Age,Sex) VALUES ('Kona','20','F')"
删除人员
<?php // Delete person $person->Id = "17"; $deleted = $person->Delete(); // Shorthand method, give id as parameter $deleted = $person->Delete(17); // SQL Equivalent "DELETE FROM persons WHERE Id = 17 LIMIT 1"
保存人员数据
<?php // Update personal data $person->Firstname = "John"; $person->Age = "20"; $person->Sex = "F"; $person->Id = "4"; // Returns affected rows $saved = $person->Save(); // Or give the bindings to the constructor $person = new person(array("Firstname"=>"John","age"=>"20","sex"=>"F","Id"=>"4")); $saved = $person->Save(); // SQL Equivalent "UPDATE persons SET Firstname = 'John',Age = 20, Sex = 'F' WHERE Id= 4"
查找人员
<?php // Find person $person->Id = "1"; $person->Find(); echo $person->firstname; // Johny // Shorthand method, give id as parameter $person->Find(1); // SQL Equivalent "SELECT * FROM persons WHERE Id = 1"
获取所有人员
<?php // Finding all person $persons = $person->all(); // SQL Equivalent "SELECT * FROM persons