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的更多相关文章

  1. Helpers\TableBuilder

    Helpers\TableBuilder Table builder helper is a class that would help you to create tables in MySQL ( ...

  2. Database API

    Database API Introduction Basic Usage Selects Joins Aggregates Raw Expressions Inserts Updates Delet ...

  3. Validation

    Validation A simple but powerful Validation Engine, in a Laravel-esque style. Its Validation Rules a ...

  4. Authentication

    Authentication Introduction Configuration Storing Passwords Authenticating Users Basic Usage Introdu ...

  5. Models

    Models Models control the data source, they are used for collecting and issuing data, this could be ...

  6. Helpers\Password

    Helpers\Password The password class uses php 5 password_ functions. To create a hash of a password, ...

  7. Helpers\FastCache

    Helpers\FastCache phpFastCache is a high-performance, distributed object caching system, generic in ...

  8. 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 ...

  9. Android_存储之DataBase之Room

    概述: Room是Google在AndroidX中提供的一个ORM(Object Relational Mapping,对象关系映射)库.它是在SQLite上提供的一个抽象层,可以使用SQLite的全 ...

随机推荐

  1. maven学习系列第二课,关于springmvc的pop.xml的依赖的添加

    不说废话了,图的书序就是操作顺序 1. 2.

  2. 导入CSV文件之后出现换行符问题

    在用sqlldr导入数据之后,出现数据无法匹配的情况 在用plsql点击的时候,发现出现换行符的情况,从而使用下面的方法进行匹配 select q.comments from q where repl ...

  3. Designing Evolvable Web API with ASP.NET 随便读,随便记 “The Internet,the World Wide Web,and HTTP”

    1982年,诞生了 Internet; 1989年,诞生了World Wide Web . "World Wide Web"的构造为主要由 三部分构成: resources 资源 ...

  4. Codevs No.1052 地鼠游戏

    2016-05-31 18:22:32 题目链接: 地鼠游戏 Codevs No.1245 题目大意: 打地鼠,一开始所有地鼠都出现,但是维持的时间(s)和击中所得的积分各不同,求出采用最优策略(1s ...

  5. MySQL 大表优化方案探讨

    当MySQL单表记录数过大时,增删改查性能都会急剧下降,可以参考以下步骤来优化: 单表优化 除非单表数据未来会一直不断上涨,否则不要一开始就考虑拆分,拆分会带来逻辑.部署.运维的各种复杂度,一般以整型 ...

  6. 第二百七十八天 how can I 坚持

    生命的意义.必须要做点什么啊.今年我们二十七八岁. 遇事不急,理清头绪就没那么复杂. 今天突然有点悔意,元旦好像应该不回去看房,花销有点大了,算了,过去的就让他过去吧,都是回忆.至少玩的挺嗨. 记住, ...

  7. Codeforces 626A Robot Sequence

    A. Robot Sequence time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  8. Spring Data JPA教程, 第八部分:Adding Functionality to a Repository (未翻译)

    The previous part of my tutorial described how you can paginate query results with Spring Data JPA. ...

  9. ASP.NET forms凭据设置和跳转的几种方法

    string user = "userName"; //默认的第1种,超时时间是在web.cofig中forms设置的timeout,单位是分钟,生成的cookie和凭证超时时间一 ...

  10. 三,对于printf函数和C语言编程的初步拓展

    前面说过了,任何程序都要有输出,所以printf函数是一个很重要的函数,所以有必要在学变量之前先拓展一下. 其实编程就是用计算机语言说话,一句一句地说,只要语法没错就能运行,至于能实现什么功能,就看编 ...