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. 纯CSS制作冒泡提示框

    来源:http://www.ido321.com/1214.html 前两天翻译了一篇文章,关于利用css的border属性制作基本图形:http://www.ido321.com/1200.html ...

  2. CentOS VPS创建pptpd VPN服务

    原文地址http://www.hi-vps.com/wiki/doku.php?id=xen_vps_centos6_install_pptpd CentOS VPS创建pptpd VPN服务 Xen ...

  3. C语言——递归练习

    1.炮弹一样的球状物体,能够堆积成一个金字塔,在顶端有一个炮弹,它坐落在一个4个炮弹组成的层面上,而这4个炮弹又坐落在一个9个炮弹组成的层面上,以此类推.写一个递归函数CannonBall,这个函数把 ...

  4. Hive1.3 JDBC连接-代码片段

    package com.hive.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Re ...

  5. python日志输出

    import logging logger = logging.getLogger() #生成一个日志对象,()内为日志对象的名字,可以不带,名字不给定就是root,一般给定名字,否则会把其他的日志输 ...

  6. flot图表的使用

    Flot是一套用Javascript写的绘制图表用的函式库, 专门用在网页上执行绘制图表功能, 由于Flot利用jQuery所以写出来的, 所以也称它为jQuery Flot ,它的特点是体积小.执行 ...

  7. HDU-3864 D_num Miller_Rabin和Pollard_rho

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3864 题意:给定一个数n,求n的因子只有四个的情况. Miller_Rabin和Pollard_rho ...

  8. centos6.4 安装erlang

    erlang官网: http://www.erlang.org 下载程序去年:

  9. Simple guide to Java Message Service (JMS) using ActiveMQ

    JMS let’s you send messages containing for example a String, array of bytes or a serializable Java o ...

  10. Lucene:信息检索与全文检索

    目录 信息检索的概念 信息检索技术的分类 全文检索与数据库查询对比 全文检索工具一般由三部分构成 全文检索中建立索引和进行检索的流程 索引里面究竟存什么 如何创建索引 如何对索引进行检索 Lucene ...