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的全 ...
随机推荐
- [Java Code] 时间维度循环生成代码片段
public static void main(String[] args) throws ParseException { String str = "20140301"; St ...
- 关于OpenCV做图像处理内存释放的一些问题
转载:http://blog.sina.com.cn/s/blog_67a7426a0101czyr.html 工程运行,发现内存持续增长,到一定的时候就发生了内存泄漏. 内存泄露的定义 内存泄露是说 ...
- Linux内存中的Cache真的能被回收么?
在Linux系统中,我们经常用free命令来查看系统内存的使用状态.在一个RHEL6的系统上,free命令的显示内容大概是这样一个状态: [root@tencent64 ~]# free ...
- Himi的base64代码
// // DataHimi.cpp // Oh!MonsterMR // // Created by Himi on 12-3-8. // Copyright (c) 2012年 Augustimp ...
- Struts2的运行流程以及关键拦截器介绍
Struts2的运行流程 1.ActionProxy是Action的一个代理类,也就是说Action的调用是通过ActionProxy实现的,其实就是调用了ActionProxy.execute()方 ...
- Linux下的JDK安装rpm命令详解
1. 安装程序 #rpm -ivh jdk-7u79-linux-x64.rpm 出现安装协议等,按接受即可. 2.设置环境变量. #vi /etc/profile JAVA_HOME=/usr/ja ...
- Spring AOP Example – Advice
Spring AOP + AspectJ Using AspectJ is more flexible and powerful. Spring AOP (Aspect-oriented progra ...
- [iOS基础控件 - 6.10.1] PickerView 餐点搭配Demo
A.需求 1.使用PickerView做出有3列餐点(水果.主菜.饮料)的搭配Demo 2.选择的餐点实时显示在“显示区” 3.提供“随机”按钮,随机选择菜品搭配 B.实现步骤 1.拖入一个Pic ...
- Codeforces Round #257 (Div. 1) C. Jzzhu and Apples (素数筛)
题目链接:http://codeforces.com/problemset/problem/449/C 给你n个数,从1到n.然后从这些数中挑选出不互质的数对最多有多少对. 先是素数筛,显然2的倍数的 ...
- HDU 2795 Billboard (线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 题目大意:有一块h*w的矩形广告板,要往上面贴广告; 然后给n个1*wi的广告,要求把广告贴 ...