indieteq/indieteq-php-my-sql-pdo-database-class

此包的最新版本(v1)没有可用的许可证信息。

基于PDO的MySQL数据库类

v1 2014-12-15 14:53 UTC

This package is not auto-updated.

Last update: 2024-09-18 18:56:36 UTC


README

PDO数据库类

一个使用PDO扩展的PHP-MySQL数据库类。

使用类

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 AND id = :id",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");
结果

使用like关键字

<?php
// Using Like 
// Notice the wildcard at the end of the value!!
$like = $db->query("SELECT * FROM Persons WHERE Firstname LIKE :firstname ", array("firstname"=>"sekit%"));
结果

获取列

<?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 !';
}

方法参数

执行查询的每个方法都有一个可选的参数,称为绑定。

rowquery方法都有一个第三个可选参数,即获取样式。默认获取样式是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 

版权和许可证

代码在啤酒许可证下发布