MysqliDb -- Simple MySQLi wrapper and object mapper with prepared statements
<hr>
### Table of Contents
**[Initialization](#initialization)**
**[Objects mapping](#objects-mapping)**
**[Insert Query](#insert-query)**
**[Update Query](#update-query)**
**[Select Query](#select-query)**
**[Delete Query](#delete-query)**
**[Insert Data](#insert-data)**
**[Insert XML](#insert-xml)**
**[Running raw SQL queries](#running-raw-sql-queries)**
**[Query Keywords](#query-keywords)**
**[Where Conditions](#where--having-methods)**
**[Order Conditions](#ordering-method)**
**[Group Conditions](#grouping-method)**
**[Properties Sharing](#properties-sharing)**
**[Joining Tables](#join-method)**
**[Subqueries](#subqueries)**
**[EXISTS / NOT EXISTS condition](#exists--not-exists-condition)**
**[Has method](#has-method)**
**[Helper Methods](#helper-methods)**
**[Transaction Helpers](#transaction-helpers)**
**[Error Helpers](#error-helpers)**
**[Table Locking](#table-locking)**
## Support Me
This software is developed during my free time and I will be glad if somebody will support me.
Everyone's time should be valuable, so please consider donating.
[Donate with paypal](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=a%2ebutenka%40gmail%2ecom&lc=DO&item_name=mysqlidb¤cy_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted)
### Installation
To utilize this class, first import MysqliDb.php into your project, and require it.
```php
require_once ('MysqliDb.php');
```
### Installation with composer
It is also possible to install library via composer
```
composer require joshcam/mysqli-database-class:dev-master
```
### Initialization
Simple initialization with utf8 charset set by default:
```php
$db = new MysqliDb ('host', 'username', 'password', 'databaseName');
```
Advanced initialization:
```php
$db = new MysqliDb (Array (
'host' => 'host',
'username' => 'username',
'password' => 'password',
'db'=> 'databaseName',
'port' => 3306,
'prefix' => 'my_',
'charset' => 'utf8'));
```
table prefix, port and database charset params are optional.
If no charset should be set charset, set it to null
Also it is possible to reuse already connected mysqli object:
```php
$mysqli = new mysqli ('host', 'username', 'password', 'databaseName');
$db = new MysqliDb ($mysqli);
```
If no table prefix were set during object creation its possible to set it later with a separate call:
```php
$db->setPrefix ('my_');
```
If connection to mysql will be dropped Mysqlidb will try to automatically reconnect to the database once.
To disable this behavoir use
```php
$db->autoReconnect = false;
```
If you need to get already created mysqliDb object from another class or function use
```php
function init () {
// db staying private here
$db = new MysqliDb ('host', 'username', 'password', 'databaseName');
}
...
function myfunc () {
// obtain db object created in init ()
$db = MysqliDb::getInstance();
...
}
```
### Multiple database connection
If you need to connect to multiple databases use following method:
```php
$db->addConnection('slave', Array (
'host' => 'host',
'username' => 'username',
'password' => 'password',
'db'=> 'databaseName',
'port' => 3306,
'prefix' => 'my_',
'charset' => 'utf8')
);
```
To select database use connection() method
```php
$users = $db->connection('slave')->get('users');
```
### Objects mapping
dbObject.php is an object mapping library built on top of mysqliDb to provide model representation functionality.
See <a href='dbObject.md'>dbObject manual for more information</a>
### Insert Query
Simple example
```php
$data = Array ("login" => "admin",
"firstName" => "John",
"lastName" => 'Doe'
);
$id = $db->insert ('users', $data);
if($id)
echo 'user was created. Id=' . $id;
```
Insert with functions use
```php
$data = Array (
'login' => 'admin',
'active' => true,
'firstName' => 'John',
'lastName' => 'Doe',
'password' => $db->func('SHA1(?)',Array ("secretpassword+salt")),
// password = SHA1('secretpassword+salt')
'createdAt' => $db->now(),
// createdAt = NOW()
'expires' => $db->now('+1Y')
// expires = NOW() + interval 1 year
// Supported intervals [s]econd, [m]inute, [h]hour, [d]day, [M]onth, [Y]ear
);
$id = $db->insert ('users', $data);
if ($id)
echo 'user was created. Id=' . $id;
else
echo 'insert failed: ' . $db->getLastError();
```
Insert with on duplicate key update
```php
$data = Array ("login" => "admin",
"firstName" => "John",
"lastName" => 'Doe',
"createdAt" => $db->now(),
"updatedAt" => $db->now(),
);
$updateColumns = Array ("updatedAt");
$lastInsertId = "id";
$db->onDuplicate($updateColumns, $lastInsertId);
$id = $db->insert ('users', $data);
```
Insert multiple datasets at once
```php
$data = Array(
Array ("login" => "admin",
"firstName" => "John",
"lastName" => 'Doe'
),
Array ("login" => "other",
"firstName" => "Another",
"lastName" => 'User',
"password" => "very_cool_hash"
)
);
$ids = $db->insertMulti('users', $data);
if(!$ids) {
echo 'insert failed: ' . $db->getLastError();
} else {
echo 'new users inserted with following id\'s: ' . implode(', ', $ids);
}
```
If all datasets only have the same keys, it can be simplified
```php
$data = Array(
Array ("admin", "John", "Doe"),
Array ("other", "Another", "User")
);
$keys = Array("login", "firstName", "lastName");
$ids = $db->insertMulti('users', $data, $keys);
if(!$ids) {
echo 'insert failed: ' . $db->getLastError();
} else {
echo 'new users inserted with following id\'s: ' . implode(', ', $ids);
}
```
### Replace Query
<a href='https://dev.mysql.com/doc/refman/5.0/en/replace.html'>Replace()</a> method implements same API as insert();
### Update Query
```php
$data = Array (
'firstName' => 'Bobby',
'lastName' => 'Tables',
'editCount' => $db->inc(2),
// editCount = editCount + 2;
'active' => $db->not()
// active = !active;
);
$db->where ('id', 1);
if ($db->update ('users', $data))
echo $db->count . ' records were updated';
else
echo 'update failed: ' . $db->getLastError();
```
`update()` also support limit parameter:
```php
$db->update ('users', $data, 10);
// Gives: UPDATE users SET ... LIMIT 10
```
### Select Query
After any select/get function calls amount or returned rows is stored in $count variable
```php
$users = $db->get('users'); //contains an Array of all users
$users = $db->get('users', 10); //contains an Array 10 users
```
or select with custom columns set. Functions also could be used
```php
$cols = Array ("id", "name", "email");
$users = $db->get ("users", null, $cols);
if ($db->count > 0)
foreach ($users as $user) {
print_r ($user);
}
```
or select just one row
```php
$db->where ("id", 1);
$user = $db->getOne ("users");
echo $user['id'];
$stats = $db->getOne ("users", "sum(id), count(*) as cnt");
echo "total ".$stats['cnt']. "users found";
```
or select one column value or function result
```php
$count = $db->getValue ("users", "count(*)");
echo "{$count} users found";
```
select one column value or function result from multiple rows:
```php
$logins = $db->getValue ("users", "login", null);
// select login from users
$logins = $db->getValue ("users", "login", 5);
// select login from users limit 5
foreach ($logins as $login)
echo $login;
```
### Insert Data
You can also load .CSV or .XML data into a specific table.
To insert .csv data, use the following syntax:
```php
$path_to_file = "/home/john/file.csv";
$db->loadData("users", $path_to_file);
```
This will load a .csv file called **file.csv** in the
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
php源码 此源码是一款视频网站系统,你可以用这个来快速搭建自己的视频网站,安装简单、界面简洁、易于使用。用户可在网站上面注册登陆上传视频、发布评论与收藏视频,系统带护眼模式,有利于保护眼睛,与此同时提供了后台管理,管理员可以进行网站名称,logo等设置。网站采用响应式设计,因此不论你在什么设备浏览网站,都有非常好的体验。 安装说明: 第一步:安装网站 (需要宝塔+apache环境,最好PHP5.5) 1、导入数据库文件夹里面的数据库文件: 2、将PHP文件夹里面的文件全部上传网站根目录 3、修改config.php文件里面的数据库信息和域名 4、完成默认账号密码是 admin/admin 第二步:生成APP 1、安装Android Studio软件,Android Studio软件和安装视频教程下载地址https://yunpan.360.cn/surl_yQ7tcLrHiRy (提取码:6776) 2、将APP源码文件夹里面的文件夹复制到桌面,在按照视频教程生成APP
资源推荐
资源详情
资源评论
收起资源包目录
精美视频网站源码 视频源码 视频网站PHP源码 支持FFMPEG 送APP源代码 (2000个子文件)
style.css 217KB
mdb.css 176KB
mdb.css 176KB
mdb.css 176KB
style.min.css 163KB
materialize.css 159KB
bootstrap.css 149KB
style.css 148KB
mdb.min.css 134KB
mdb.min.css 134KB
mdb.min.css 134KB
bootstrap.css 122KB
bootstrap.css 122KB
bootstrap.css 122KB
bootstrap.min.css 118KB
bootstrap.min.css 113KB
materialize.min.css 112KB
bootstrap.min.css 90KB
bootstrap.min.css 90KB
bootstrap.min.css 90KB
material-design-iconic-font.css 88KB
animate.css 74KB
twemoji-awesome.css 71KB
material-design-iconic-font.min.css 69KB
animate.min.css 52KB
skin.min.css 42KB
font-awesome.css 37KB
style.css 33KB
font-awesome.min.css 30KB
jquery-ui.min.css 29KB
skin.mobile.min.css 27KB
all-themes.css 26KB
bootstrap-theme.css 26KB
ambiance.css 26KB
bootsrap-rtl.min.css 24KB
sweetalert.css 23KB
bootstrap-theme.min.css 23KB
all-themes.min.css 21KB
sweetalert2.css 19KB
night-mode.css 15KB
sweetalert2.min.css 14KB
mediaelementplayer.min.css 11KB
rtl.style.css 10KB
materialize.css 10KB
imgshare.css 10KB
codemirror.css 8KB
shCoreEclipse.css 8KB
magnific-popup.css 8KB
shCoreDjango.css 7KB
shCoreFadeToGrey.css 7KB
shCoreDefault.css 7KB
shCoreMidnight.css 7KB
shCoreRDark.css 7KB
shCoreEmacs.css 7KB
shCoreMDUltra.css 7KB
bootstrap-select.css 7KB
bootstrap-select.min.css 6KB
bootstrap-select.min.css 6KB
slide_share.css 6KB
visualblocks.css 5KB
solarized.css 5KB
shCore.css 5KB
mdn-like.css 5KB
bootstrap-colorpicker.css 5KB
waves.css 5KB
share_popup.css 5KB
mejs-dark.css 4KB
share_style2_16.css 4KB
share_style2_24.css 4KB
share_style1_16.css 4KB
share_style1_24.css 4KB
like.css 4KB
dataTables.bootstrap.css 4KB
dataTables.bootstrap.css 4KB
share_style0_16.css 4KB
docs.css 4KB
bootstrap-colorpicker.min.css 4KB
liquibyte.css 4KB
waves.min.css 4KB
content.min.css 4KB
share_style1_32.css 4KB
share_style2_32.css 4KB
share_style0_24.css 4KB
share_style0_32.css 4KB
dataTables.bootstrap.min.css 3KB
dataTables.bootstrap.min.css 3KB
content.inline.min.css 3KB
notifIt.min.css 3KB
merge.css 3KB
notifIt.css 3KB
xq-dark.css 3KB
lint.css 3KB
select_share.css 3KB
tagit.ui-zendesk.css 3KB
duotone-light.css 3KB
lesser-dark.css 3KB
duotone-dark.css 3KB
shThemeEclipse.css 3KB
icecoder.css 2KB
material.css 2KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
行动之上
- 粉丝: 1973
- 资源: 931
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功