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的全 ...
随机推荐
- C++实现网格水印之调试笔记(五)—— 提取出错
在实现提取水印的过程中,遇到了一些问题 首先还是根据论文中的思路来梳理一下整个提取流程 读入两个模型,一个原始模型ori_mesh, 一个水印模型wm_mesh. 将两个模型对齐(即放在同一个坐标系下 ...
- [置顶] Kendo UI开发教程: Kendo UI 示例及总结
前面基本介绍完Kendo UI开发的基本概念和开发步骤,Kendo UI的示例网站为http://demos.kendoui.com/ ,包含了三个部分 Web DemoMobile DemoData ...
- git使用中遇到的常见问题
.gitignore 中添加的文件不能被忽略掉 这是因为我们误解了 .gitignore 文件的用途,该文件只能作用于 Untracked Files,也就是那些从来没有被 Git 记录过的文件(自添 ...
- Apache Spark GraphX的使用简介
类似 Spark 在 RDD 上提供了一组基本操作符(如 map, f ilter, reduce), GraphX 同样也有针对 Graph 的基本操作符,用户可以在这些操作符传入自定义函数和通过修 ...
- ocp 1Z0-051 141-175题解析
141. View the Exhibitand examine the structure of CUSTOMERS and GRADES tables. You need to displayna ...
- HDU 4950 Monster (水题)
Monster 题目链接: http://acm.hust.edu.cn/vjudge/contest/123554#problem/I Description Teacher Mai has a k ...
- POJ3126 Prime Path
http://poj.org/problem?id=3126 题目大意:给两个数四位数m, n, m的位数各个位改变一位0 —— 9使得改变后的数为素数, 问经过多少次变化使其等于n 如: 10331 ...
- MVC架构和SSH框架对应关系
MVC三层架构:模型层(model),控制层(controller)和视图层(view).模型层,用Hibernate框架让来JavaBean在数据库生成表及关联,通过对JavaBean的操作来对数据 ...
- codeforce 630N Forecast
N. Forecast time limit per test 0.5 seconds memory limit per test 64 megabytes input standard input ...
- java 指导 (Java Tutorial)
case1: site:docs.oracle.com -xmx -xms case2: site:docs.oracle.com thread case3: site:docs.oracle.com ...