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的全 ...
随机推荐
- 快速上手Android数据库操作
Android采用关系型数据库SQLite3,它是一个支持SQL轻量级的嵌入式数据库,在嵌入式操作系统上有很广泛的应用,WM采用的也是SQLite3 关于过于.原理方面的东西在这篇文章里不会提到,但是 ...
- leetcode—Plus one
1.题目描述 Given a number represented as an array of digits, plus one to the number. 2.解法分析 不要被常规思路限制住 ...
- 搭建Titanium开发环境
轻松制作 App 再也不是梦! Titanium Mobile 让你能够使用你所熟悉的 web 技术,制作出如同使用Objective-C 或 Java 写出的 Native App. 除了有多达三百 ...
- 【转】UIBezierPath精讲
http://www.henishuo.com/uibezierpath-draw/ 基础知识 使用UIBezierPath可以创建基于矢量的路径,此类是Core Graphics框架关于路径的封装. ...
- HDU 5531 Rebuild (2015长春现场赛,计算几何+三分法)
Rebuild Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total S ...
- 典型LoadRunner脚本
Action() { int rc = 0; int cmp_result = 0; char over_msg[] = "\"真遗憾,好心塞,手慢了一下,已经被人抢走了,再去看看 ...
- CodeForces 732D Exams (二分)
题意:某人要考试,有n天考m个科目,然后有m个科目要考试的时间和要复习多少天才能做,问你他最早考完所有科目是什么时间. 析:二分答案,然后在判断时,直接就是倒着判,很明显后出来的优先,也就是一个栈. ...
- Unity3D之空间转换学习笔记(三):3D数学
3D数学基础 向量 向量可以看做具有方向和大小的一条线段. 比如:我们如果用点A减去点B,则可以得到一个向量,该向量的方向为点B面向点A的方向,而大小为两点的距离.这个方法在游戏开发中经常用到,比如我 ...
- [置顶] a+=1/a=+1/a-=1区别-c语言
1.解释 a+=1/a=+1/a-=1 含义 a+=1 实质等于 a += 1,也就是等于 a = a + 1: a=+1 实质等于 a = +1:[因为运算符中没有=+,很多人误以为是 a =+ 1 ...
- 使用SVG Path绘图
最近一个项目,需要做个Web版本的设计器,用来进行工厂流水线布局的设计. 项目中采用了SVG.JS来做,但是以前流水线是采用单纯的画线的方式实现.客户提出希望用不同的底纹表示不同的流水线,经过一番调查 ...