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. [原创]cocos2d-x + Lua接入iOS原生SDK的实现方案

    相信很多朋友在使用cocos2d-x+lua开发游戏时都遇到过接入iOS原生SDK的问题,比如常见的接应用内支付SDK,广告SDK或是一些社交平台SDK等等,我也没少接过这类SDK.这篇文章主要是对我 ...

  2. Orchard源码分析(1):插件式的支持——模块和主题

    在Orchard,模块和主题都是可以插拔式的,在源码处理时,用类型(参考:DefaultExtensionTypes)区分,都没太大的本质区别,以下都称做模块. 插件的支持,实现分以下几步: 搜集模块 ...

  3. Java——观察者模式实例

    观察者模式(订阅/发布模式) 作者: 代码大湿 代码大湿 Java中观察者模式中主要是Observerable类(被观察者),和Observer接口(观察者).下面是个简单的demo //被观察者 p ...

  4. 非常实用的Ubuntu常用终端命令

    先介绍关于文件和目录的命令: ls 列出当前目录文件(不包括隐含文件) ls -a 列出当前目录文件(包括隐含文件) ls -l 列出当前目录下文件的详细信息 cd .. 回当前目录的上一级目录 cd ...

  5. Android 不同应用通过SharedPreference实现共享数据

    Android不同应用之间数据的共享有许多方式,但是我觉得还是使用sharedPreference比较简单和轻量级.如果程序B想要访问程序A的sharedPreference可以通过下面的语句来实现: ...

  6. H264编码参数的一些小细节

    一次写播放器,基于ijkplayer.在播放一些网络视频的时候,发现无论怎么转码,视频比例始终不对.即便获取了分辨率,但是播放的时候,view不是分辨率比例的那个长宽比.使用ffmpeg查看了一下属性 ...

  7. StringReplace用法

    来自:http://www.aspww.cn/View/12022801.aspx ---------------------------------------------------------- ...

  8. RHEL安装配置JAVA

    查看当前java版本 [root@esb-mmplus-04 ~]# java -version java version "1.6.0_24" OpenJDK Runtime E ...

  9. 几个代码片段-计算程序运行时间+获得当前目录+生成MD5

    计算程序运行时间 long startTime = System.currentTimeMillis(); System.out.println("程序运行时间: " + (Sys ...

  10. Spring入门(3)-Spring命名空间与Bean作用域

    Spring入门(3)-Spring命名空间与Bean作用域 这篇文章主要介绍Spring的命名空间和Bean作用域 0. 目录 Spring命名空间 Bean作用域 1. Spring命名空间 在前 ...