Helpers\Database
Helpers\Database
The database class is used to connect to a MySQL database using the connection details set in the app/Config.php.
The constants (DB_TYPE, DB_HOST, DB_NAME, DB_USER, DB_PASS) are used to connect to the database, the class extends PDO, it can pass the connection details to its parent construct.
try {
parent::__construct(DB_TYPE.':host='.DB_HOST.'; dbname='.DB_NAME,DB_USER,DB_PASS);
$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
Logger::newMessage($e);
customErrorMsg();
}
The error mode is set to use exceptions rather than failing silently in the event of an error. If there is an error they are recorded in app/logs/error.log
This class has the following methods:
NOTE: The methods use prepared statements.
Select
The SELECT query below will return a result set based upon the SQL statement. The result set will return all records from a table called contacts.
$this->db->select("SELECT firstName, lastName FROM ".PREFIX."contacts");
Optionally an array can be passed to the query, this is helpful to pass in dynamic values, they will be bound to the query in a prepared statement this ensures the data never goes into the query directly and avoids any possible sql injection.
Example with passed data:
$this->db->select("SELECT firstName, lastName FROM ".PREFIX."contacts WHERE contactID = :id", array(':id' => $id));
In this example there is a where condition, instead of passing in an id to the query directly a placeholder is used :id then an array is passed the key in the array matches the placeholder and is bound, so the database will get both the query and the bound data.
Insert
The insert method is very simple it expects the table name and an array of data to insert:
$this->db->insert(PREFIX.'contacts', $data);
The data array is created in a controller, then passed to a method in a model
$postdata = array(
'firstName' => $firstName,
'lastName' => $lastName,
'email' => $email
);
$this->model->insertContact($postdata);
The model passes the array to the insert method along with the name of the table, optionally return the id of the inserted record back to the controller.
public function insertContact($data)
{
$this->db->insert(PREFIX.'contacts', $data);
return $this->db->lastInsertId('contactID');
}
Update
The update is very similar to insert an array is passed with data, this time also an identifier is passed and used as the where condition.
Controller:
$postdata = array(
'firstName' => $firstName,
'lastName' => $lastName,
'email' => $email
);
$where = array('contactID' => $id);
$this->model->updateContact($postdata, $where);
Model:
public function updateContact($data, $where)
{
$this->db->update(PREFIX.'contacts',$data, $where);
}
Delete
This method expects the name of the table and an array containing the columns and value for the where claus.
Example array to pass:
$data = array('contactID' => $id);
Delete in model
public function deleteContact($data)
{
$this->db->delete(PREFIX.'contacts', $data);
}
Truncate
This method will delete all rows from the table, the method expects the table name as an argument.
public function deleteContact($table)
{
$this->db->truncate($table);
}
Helpers\Database的更多相关文章
- Helpers\TableBuilder
Helpers\TableBuilder Table builder helper is a class that would help you to create tables in MySQL ( ...
- Database API
Database API Introduction Basic Usage Selects Joins Aggregates Raw Expressions Inserts Updates Delet ...
- Validation
Validation A simple but powerful Validation Engine, in a Laravel-esque style. Its Validation Rules a ...
- Authentication
Authentication Introduction Configuration Storing Passwords Authenticating Users Basic Usage Introdu ...
- Models
Models Models control the data source, they are used for collecting and issuing data, this could be ...
- Helpers\Password
Helpers\Password The password class uses php 5 password_ functions. To create a hash of a password, ...
- Helpers\FastCache
Helpers\FastCache phpFastCache is a high-performance, distributed object caching system, generic in ...
- ASP.NET MVC- VIEW Creating Custom HTML Helpers Part 2
The goal of this tutorial is to demonstrate how you can create custom HTML Helpers that you can ...
- Android_存储之DataBase之Room
概述: Room是Google在AndroidX中提供的一个ORM(Object Relational Mapping,对象关系映射)库.它是在SQLite上提供的一个抽象层,可以使用SQLite的全 ...
随机推荐
- linux-制作linux启动U盘
1. 使用的制作工具 Ø 下载需要制作启动盘的linux的iso文件 Ø 制作启动盘的软件linux usb creater Ø U盘(大小差不多需要4G的空间) 软件可以的下载的地址:http:// ...
- BootStrap入门教程 (二) :BASE CSS(排版(Typography),表格(Table),表单(Forms),按钮(Buttons))
上讲回顾:Bootstrap的手脚架(Scaffolding)提供了固定(fixed)和流式(fluid)两种布局,它同时建立了一个宽达940px和12列的格网系统. 基于手脚架(Scaffoldin ...
- 【转】Maven实战(一)---Maven Build--缺少Jar包
原博文出于: http://blog.csdn.net/liutengteng130/article/details/41426955 感谢! 新建的Maven项目,在build的时候总是打包失败 ...
- 【转】你真的了解iOS代理设计模式吗?
转自:http://www.cocoachina.com/ios/20160317/15696.html 在项目中我们经常会用到代理的设计模式,这是iOS中一种消息传递的方式,也可以通过这种方式来传递 ...
- Xcode 的正确打开方式——Debugging
程序员日常开发中有大量时间都会花费在 debug 上,从事 iOS 开发不可避免地需要使用 Xcode.这篇博客就主要介绍了 Xcode 中几种能够大幅提升代码调试效率的方式. “If debuggi ...
- jdk的wsimport方法实现webservice客户端调用服务
1.配置好jdk环境,打开命令行,输入wsimport回车能看到很多该命令的参数, -s:要生成客户端代码的存储路径 -p:对生成的代码从新打包 这两个最常用. 在打开的命令行中输入:wsimport ...
- linux系统日常管理
笔者在前面介绍的内容都为linux系统基础类的,如果你现在把前面的内容全部很好的掌握了,那最好了.不过笔者要说的是,即使你完全掌握了,你现在还是不能作为一名合格的linux系统管理员的,毕竟系统管理员 ...
- js中的if判断十分优美的简洁写法
本尊混迹猿人类也有5年有余,从最开始的C#到java再到php到至今的python,不能说精通,也算得上是熟悉,对各个语言的语法也算是了解. 虽然目前在开发web程序,了解一些java知识,但是今天在 ...
- 【灵感】wifi通过wifi发送优惠信息
1.[灵感]wifi通过wifi发送优惠信息 http://content.businessvalue.com.cn/post/15362.html 2.手机彩票大爆发 http://content. ...
- secureCRT使用VIM时对语法高亮
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html 内部邀请码:C8E245J (不写邀请码,没有现金送) 国 ...