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的全 ...
随机推荐
- LeetCode题解——Palindrome Number
题目: 判断一个数字是不是回文数字,即最高位与最低位相同,次高位与次低位相同,... 解法: 求出数字的位数,然后依次求商和求余判断是否相等. 代码: class Solution { public: ...
- 百家搜索:在网站中添加Google、百度等搜索引擎
来源:http://www.ido321.com/1143.html 看到一些网站上添加了各种搜索引擎.如Google.百度.360.有道等,就有点好奇,这个怎么实现?研究了一各个搜索引擎怎么传送关键 ...
- 精妙SQL语句 基础
精妙SQL语句SQL语句先前写的时候,很容易把一些特殊的用法忘记,我特此整理了一下SQL语句操作,方便自己写SQL时方便一点,想贴上来,一起看看,同时希望大家能共同多多提意见,也给我留一些更好的佳句, ...
- Java 从单核到多核的多线程(并发)
JAVA 并发编程 最初计算机是单任务的,然后发展到多任务,接着出现多线程并行,同时计算机也从单cpu进入到多cpu.如下图: 多任务:其实就是利用操作系统时间片轮转使用的原理.操作系统通 ...
- ESXi 与其它虚拟化底层产品之比较:
序号 虚拟化管理程序属性 VMware ESXi 5.0 采用 Hyper-V 的 Windows Server 2008 R2 SP1 Citrix XenServer 5.6 FP1 1 磁盘占 ...
- Junit。。。
keep the bar green to keep the code clean.
- AHOI2013 Round2 Day1 简要题解
第一题,好吧这是个dp.(搜素也能在BZOJ上卡过). 第二题,BFS搜索碰到的立方体面数,智硬没有想到... 第三题,其实一看就有思路,但关键是求x坐标不交的矩形对数+y坐标不交的矩形对数 - x, ...
- 安卓升级提示 phoneGap APK软件更新提示
以下代码由PHP200 阿杜整理 package com.example.syzx; import java.io.BufferedReader; import java.io.File; imp ...
- POJ 1679 The Unique MST (最小生成树)
The Unique MST 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/J Description Given a conn ...
- #JAVA操作LDAP
package com.wisdombud.unicom.monitor.ldap; import java.util.ArrayList; import org.slf4j.Logger; impo ...