softon/mysqli-dbclass

一种简单的面向对象的方法,用于访问 MySQL 数据库。

v1.0.0 2015-08-07 10:38 UTC

This package is auto-updated.

Last update: 2024-09-13 23:39:13 UTC


README

MySQLi 数据库类,适用于 PHP 开发者

一种简单的面向对象的方法,用于访问 MySQL 数据库。这个类将使您的产品开发更加轻松,并节省在创建简单的 CRUD 操作时浪费的大量时间。此类还支持事务。良好的错误报告和调试功能。额外的快速命令,提供更好的安全性和发展便利。请参考 example.php 以获取所有示例代码。

这个类是 ricocheting.com 的 PHP MySQL 包装器 v3 的增强版本,链接为:ricocheting.com。因此,它与该类兼容。要使用增强的 MySQL 包装器,只需在您的代码中包含 Database.class.php 文件,一切应该都会像以前一样工作。

如何设置

在开始之前,编辑 config.inc.php 以匹配您的数据库凭据。然后,在代码顶部包含类文件和配置文件,您想要使用类的位置,如下所示。


include_once('config.inc.php');
include_once('Database.php');

创建一个 DB 对象以与类交互。


$db = new Database(DB_SERVER,DB_USER,DB_PASS,DB_DATABASE);

您现在可以使用此 $db 对象运行任何查询。如果您在一个函数或另一个类内部,您可以调用 obtain 函数以恢复 $db 对象。


$db = new Database::obtain();

如何使用

//Simple Raw Query
$result = $db->query("SELECT * FROM authors")->fetch();

//Parametrised Query and fetch one Record
$result = $db->query("SELECT * FROM :table WHERE :name = ':value'",['table'=>'authors','name'=>'paperid','value'=>'P12206436'])->fetch();

//Parametrised Query and fetch all Records
$result_array = $db->query("SELECT * FROM :table WHERE :name = ':value'",['table'=>'authors','name'=>'paperid','value'=>'P12206436'])->fetch_all();

// Find a row by id and fetch
$result = $db->findById('authors',1);

// Find a row by column and fetch
$result = $db->findByCol('authors','paperid','P12206436');


// Count All rows in a table
$result = $db->count('authors');

// Count All rows in a table with condition
$result = $db->count('authors'," astatus='Approved'");

// Update a row
$result = $db->update('authors',['paperid'=>'SHIBU']," id = '1'");

// Update a row with parameter protection
$result = $db->update('authors',['paperid'=>'SHIBU','aname'=>'test','aorg'=>'test']," id = '1'",['paperid','aname']);

// Bulk Update a row with parameter protection without transaction
$result = $db->bulk_update('authors',array(
    1=> array('paperid'=>'SHIBU','aname'=>'test','aorg'=>'test'),
    2=> array('paperid'=>'SHIBU2','aname'=>'test2','aorg'=>'test2')
),['paperid','aname']);


// Bulk Update a row without parameter protection with transaction
$result = $db->bulk_update('authors',array(
    1=> array('paperid'=>'SHIBU','aname'=>'test','aorg'=>'test'),
    2=> array('paperid'=>'SHIBU2','aname'=>'test2','aorg'=>'test2')
),null,true);

// Insert Data
$result = $db->insert('authors',['paperid'=>rand(1234,99999),'aname'=>'Test Ing']);

// Bulk Insert with parameter protection with transaction
$result = $db->bulk_insert('authors',array(
    array('paperid'=>'SHIBU','aname'=>'test','aorg'=>'test'),
    array('paperid'=>'SHIBU2','aname'=>'test2','aorg'=>'test2')
),['paperid','aname'],true);


// Delete a record
$result = $db->delete('authors'," id=1 ");