sinret/pdo
PDO包装器
dev-master
2015-11-14 14:49 UTC
Requires
- php: >=5.4.0
This package is not auto-updated.
Last update: 2024-09-18 18:17:21 UTC
README
1. 使用composer添加
composer require sintret/pdo
2. 扩展你的类db和对象模型
#Create class MyDb for example and extends from \sintret\pdo\Db.
#To use the class
#if you want object model from table mysql you can use like these following class Persons with extends from Crud
require(__DIR__ . './config/database.php');
class MyDb extends \sintret\pdo\Db {
public function __construct() {
parent::__construct(HOST, DB_NAME, DB_USER, DB_PASS);
}
}
//create class Persons if we want
class Persons extends sintret\pdo\Crud {
protected $table = 'persons';
# Primary Key of the table
protected $pk = 'id';
public $className = 'MyDb';
}
3. 在你的项目中的index.php示例中进行引导
<?php require(__DIR__ . '/vendor/autoload.php'); include 'MyDb.php'; $db = new MyDb; $persons = $db->query("SELECT * FROM persons"); echo "<pre>";print_r($persons); $person = new Persons(); // Create new person $person->Firstname = "Kona"; $person->Age = "20"; $person->Sex = "F"; $created = $person->create(); // Or give the bindings to the constructor $person = new Persons(array("Firstname" => "Kona", "age" => "20", "sex" => "F")); $created = $person->create();
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");
结果
删除/更新/插入
当通过query方法执行删除、更新或插入语句时,将返回受影响的行。
<?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方法有一个第三个可选参数,即fetch样式。默认的fetch样式是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 fetchstyle的更多信息: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