此包已被弃用且不再维护。未建议替代包。

JKUAT App 数据库接口

v1.0.1 2015-04-11 14:17 UTC

This package is not auto-updated.

Last update: 2022-02-01 12:46:35 UTC


README

angrycoders/system 和生态系统提供数据库驱动程序。

安装

通过 Composer

$ composer require angrycoders/db

获取主分支更新

$ composer require "angrycoders/db": "dev-master"

获取特定版本

$ composer require "angrycoders/db": "1.0.0"

使用方法

请确保您的服务器首先正在运行。

require_once __DIR__ . '../vendor/autoload.php';

use AngryCoders\Db\Db;

use AngryCoders\Db\DbException;

$db = new Db();

添加新表

$db->createTable(tableName, fields)

在数据库中创建一个新表(如果尚不存在)。

  • tableName(字符串):表名
  • fields(数组):关联数组描述表属性。请见 以下

fields 传递给 createTable

 $fields = array(
    fieldName1 => array(type, size [, extraAttrb1, extraAttrb2, ... , ...]),
    fieldName2 => array(type, size [, extraAttrib1, extraAttrb2, ... , ...]),
    ...)
  • fieldName(字符串):属性名
  • type(常量):必需。 由库公开的常量。包括
    • Db::DATETIME
    • Db::DOUBLE
    • Db::FLOAT
    • Db::INTEGER
    • Db::TEXT
    • Db::VARCHAR
  • size(整数):必需。 字段长度。如果一个字段没有大小,只需指定零(0)。
  • extraAttrb1, extraAttrb2, ...可选。 任何其他要应用的属性。例如,
    • Db::AUTO_INCREMENT
    • Db::NOT_NULL
    • Db::PRIMARY_KEY

示例

try {
    $db->createTable("student", array(
            "studentID" => array(Db::INTEGER, 11, Db::PRIMARY_KEY, Db::AUTO_INCREMENT),
            "regNo" => array(Db::VARCHAR, 20),
            "accountID" => array(Db::INTEGER, 11),
            "name" => array(Db::VARCHAR, 50)));
} catch (DbException $e) {
    echo $e;
}

删除表

$db->deleteTable(tableName);

从数据库中删除一个表。

  • tableName(字符串):表名

示例

$db->deleteTable("tableName");

插入记录

$db->insertRecord(tableName, values)

将记录插入目标表。

  • tableName(字符串):表名
  • values(数组):要插入的值数组。

示例

$db -> insertRecord("student", array(NULL, 'cs281-3722/2013', '54', '23', 'Magani Felix'));

删除记录

$db->deleteRecord(tableName, value, field)

从表 tableName 中删除记录,其中 field 等于 value

  • tableName(字符串):表名
  • value(任何):要比较的值
  • field(字符串):用于搜索 value 的字段名

示例

$db->deleteRecord("student", 'cs281-3722/2013', "regNo");

选择记录

$db->getRecord(tableName, field, value [, targetFields])

从表 tableName 中选择所有记录,其中字段 field 包含 value

  • tableName(字符串):表名
  • field(字符串):用于搜索 value 的字段名
  • value(任何):要比较的值
  • targetFields(数组):可选。要返回值的字段名数组。如果没有传递,将使用所有字段。

示例

$result = $db->getRecord("student", 'name', 'Felix');

foreach($result as $row)
{
    echo $row['studentID'] . "<br>";
    echo $row['regNo'] . "<br>";
    echo $row['courseID'] . "<br>";
    echo $row['name'] . "<br>";
    echo $row['accountID'] . "<br><br>";
}

$result = $db->getRecord("student", 'name', 'Felix', array('name','accountID'));
foreach($result as $row)
{
    echo $row['name'] . "<br>";
    echo $row['accountID'] . "<br>";
}

获取记录

$db->getAllRecords(tableName [, targetFields [, numOfRecords [, startIndex]]])

返回表 tableName 中的所有记录。

  • tableName(字符串):表名
  • targetFields(数组):可选。要返回值的字段名数组。如果没有传递,将使用所有字段
  • numOfRecords(整数):可选。要返回的记录数
  • startIndex(整数):可选。从哪个索引开始获取记录

示例

$result = $db->getAllRecords("student");

foreach($result as $row)
{
    echo $row['studentID'] . "<br>";
    echo $row['regNo'] . "<br>";
    echo $row['courseID'] . "<br>";
    echo $row['name'] . "<br>";
    echo $row['accountID'] . "<br><br>";
}

//selecting accountID and name fields only to be returned
$result = $db->getAllRecords("student", array('name','accountID'));

//selecting the first 5 records to be returned
$result = $db->getAllRecords("student", null, 5);

//selecting the next 10 records to be returned from the 5th record
$result = $db->getAllRecords("student", null, 10, 4);

更新记录

$db->updateRecord(tableName, fields, values, discriminantField, discriminantValue)

在表 tableName 中更新字段 fields 中的记录,值为 values,其中字段 discriminantField 等于 discriminantValue

  • tableName(字符串):表名
  • fields(数组):要针对的目标表字段数组
  • values(数组):要更新的值的数组
  • discriminantField(字符串):用于比较记录的字段名称
  • discriminantValue(任意类型):用于比较的值

示例

$tableName = "student"; //The name of the table
$fields = array('name', 'accountID'); //Fields to be updated
$values = array('John Doe', '23'); //The new values
$field = 'regNo'; //The field to check
$value = 'cs281'; // The value of the field to check

$db->updateRecord($tableName, $fields, $values, $field, $value);

测试

$ phpunit

贡献

请参阅CONTRIBUTING.md以获取详细信息。

安全

如果您发现任何与安全相关的问题,请通过电子邮件keysindicet@gmail.com联系,而不是使用问题跟踪器。

鸣谢

Magani Felix贡献者

许可

MIT 许可证(MIT)。请参阅LICENSE.md以获取更多信息。