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)**
**[Pagination](#pagination)**
**[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 thingengineer/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 .cs
没有合适的资源?快使用搜索试试~ 我知道了~
基于脉聊二开版本仿Twitter源代码 社交网络平台源码
共2000个文件
svg:2530个
php:608个
scssc:230个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
5星 · 超过95%的资源 1 下载量 32 浏览量
2022-04-13
15:08:44
上传
评论 3
收藏 107.61MB ZIP 举报
温馨提示
这是一款类似于 Twitter 网站的源代码。利用原代码,你可以快速搭建自己的社交网络平台。 程序语言是 PHP。数据库采用 Mysql。与此同时系统带有注册,评论,转发等功能。 注册用户可以分享他们的图片视频,或者创建投票的。同时系统自带 ffmpeg 功能上传视频会自动获取封面。还自带域名解析功;分享网址会抓取网站的描述和标题。配合后台管理面板,你可以快速设置所有的一切。 网站的logo和图标地址在 themes/default/statics/img 文件夹里面你可以替换你的图标或logo。
资源推荐
资源详情
资源评论
收起资源包目录
基于脉聊二开版本仿Twitter源代码 社交网络平台源码 (2000个子文件)
web.config 6KB
style.master.css 189KB
bootstrap.min.css 152KB
bootstrap-v4.0.0.min.css 141KB
style.css 120KB
bootstrap.min.css 107KB
style.master.css 75KB
animate.min.css 57KB
style.master.css 51KB
style.master.css 46KB
style.dark.css 37KB
style.master.css 32KB
style.master.css 29KB
style.master.css 26KB
emojionearea.min.css 26KB
emojionearea.css 26KB
style.master.css 22KB
style.master.css 19KB
style.master.css 19KB
jquery.fancybox.css 17KB
style.master.css 16KB
install.master.style.css 15KB
style.master.css 15KB
waitMe.min.css 14KB
style.master.css 12KB
style.mq.css 11KB
style.dark.css 11KB
bootstrap-select-v1.13.9.min.css 10KB
materialize.css 10KB
materialize.css 10KB
style.css 9KB
md-checkbox.css 8KB
style.master.css 8KB
style.master.css 8KB
shCoreEclipse.css 8KB
style.master.css 7KB
shCoreDjango.css 7KB
shCoreFadeToGrey.css 7KB
shCoreDefault.css 7KB
shCoreMidnight.css 7KB
shCoreRDark.css 7KB
shCoreEmacs.css 7KB
shCoreMDUltra.css 7KB
style.dark.css 6KB
style.min.css 6KB
style.dark.css 6KB
style.dark.css 6KB
shCore.css 5KB
style.dark.css 5KB
style.mq.css 5KB
owl.carousel.css 5KB
style.master.css 5KB
style.dark.css 4KB
style.dark.css 4KB
style.dark.css 3KB
owl.carousel.min.css 3KB
style.dark.css 3KB
style.master.css 3KB
style.dark.css 3KB
style.mq.css 3KB
shThemeEclipse.css 3KB
default.css 2KB
style.dark.css 2KB
style.mq.css 2KB
getid3.css 2KB
shThemeDjango.css 2KB
style.master.css 2KB
style.master.css 2KB
style.master.css 2KB
shThemeFadeToGrey.css 2KB
shThemeVisualStudio.css 2KB
shThemeDefault.css 2KB
shThemeMidnight.css 2KB
shThemeRDark.css 2KB
style.dark.css 2KB
shThemeEmacs.css 2KB
shThemeMDUltra.css 2KB
style.dark.css 2KB
style.mq.css 2KB
style.dark.css 2KB
shThemeAppleScript.css 2KB
preloader.min.css 2KB
style.mq.css 2KB
style.master.css 1KB
owl.theme.default.css 1KB
style.dark.css 1KB
owl.theme.green.css 1KB
style.mq.css 1KB
style.mq.css 1KB
style.mq.css 1KB
owl.theme.green.min.css 1013B
owl.theme.default.min.css 1013B
style.mq.css 965B
hightlightjs-dark.css 773B
material_icon.css 530B
material_icon.css 530B
style.mq.css 499B
style.dark.css 440B
style.dark.css 378B
style.dark.css 374B
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
- m0_722231622022-07-09感谢大佬分享的资源,对我启发很大,给了我新的灵感。
李传海
- 粉丝: 208
- 资源: 367
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 企业文化的三层结构.docx
- 企业文化概念的探讨.docx
- 企业文化就是一言一行.docx
- 企业文化与东西方文化传统.docx
- 基于java的网上教学辅助系统的设计与实现论文.doc
- Be Like a Goldfish, Don't Memorize! Mitigating Memorization in Generative LLMs
- 从《士兵突击》学管理之企业文化.doc
- 论企业文化和企业制度.doc
- 浅论人力资源管理与企业文化的关系.doc
- 王伟论企业文化.doc
- 传统文化与企业家精神.docx
- 陈亮:企业文化建设的核心是认同.docx
- 传承与发展——企业家与企业文化之辩证关系.docx
- 董明珠:用企业文化留住人.docx
- 李东生首谈企业文化 TCL管理方式正在变革.docx
- 蒋锡培:社会公益需要构建慈善的企业文化.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功