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.

  1. try {
  2. parent::__construct(DB_TYPE.':host='.DB_HOST.'; dbname='.DB_NAME,DB_USER,DB_PASS);
  3. $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  4. } catch (PDOException $e) {
  5. Logger::newMessage($e);
  6. customErrorMsg();
  7. }

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.

  1. $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:

  1. $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:

  1. $this->db->insert(PREFIX.'contacts', $data);

The data array is created in a controller, then passed to a method in a model

  1. $postdata = array(
  2. 'firstName' => $firstName,
  3. 'lastName' => $lastName,
  4. 'email' => $email
  5. );
  6. $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.

  1. public function insertContact($data)
  2. {
  3. $this->db->insert(PREFIX.'contacts', $data);
  4. return $this->db->lastInsertId('contactID');
  5. }

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:

  1. $postdata = array(
  2. 'firstName' => $firstName,
  3. 'lastName' => $lastName,
  4. 'email' => $email
  5. );
  6. $where = array('contactID' => $id);
  7. $this->model->updateContact($postdata, $where);

Model:

  1. public function updateContact($data, $where)
  2. {
  3. $this->db->update(PREFIX.'contacts',$data, $where);
  4. }

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:

  1. $data = array('contactID' => $id);

Delete in model

  1. public function deleteContact($data)
  2. {
  3. $this->db->delete(PREFIX.'contacts', $data);
  4. }

Truncate

This method will delete all rows from the table, the method expects the table name as an argument.

  1. public function deleteContact($table)
  2. {
  3. $this->db->truncate($table);
  4. }

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. 关于SecureCRT的安装和破解问题以及xp系统的串口问题

    今天下午找了几个小时的软件,因为交叉编译环境要搭好,其中SecureCRT还有串口问题要解决,我突然间发现我开始光盘中的SecureCRT坏掉了,在网站上下载SecureCRT,结果要很多的积分,这样 ...

  2. 关于在Eclipse里面启动了服务,但是localhost:8080无法访问的问题:

    今天eclipse重新换了一个然后写项目,结果发生了一些bug,当在Tomca服务开启之后,浏览器端输入localhost:8080无法访问,以为是服务器没有搞定,检查了没问题,百度了一下有很多乱七八 ...

  3. bzoj 3505 [Cqoi2014]数三角形(组合计数)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3505 [题意] 在n个格子中任选3点构成三角形的方案数. [思路] 任选3点-3点共线 ...

  4. 【暑假】[实用数据结构]UVa11995 I Can Guess the Data Structure!

    UVa11995  I Can Guess the Data Structure! 思路:边读边模拟,注意empty的判断! 代码如下: #include<iostream> #inclu ...

  5. 有关android UI 线程

    1. GUI线程框架 常见的 Swing, SWT框架都是作为单线程子系统来实现的,实际上不仅限于在Java中, Qt.MacOS Cocoa以及其他的环境中的GUI框架都是单线程的.虽然很多人尝试过 ...

  6. 在linux上用dd命令实现ghost功能

    ghost和g4l 安装操作系统,速度太慢,整个过程太冗长乏味了. 安装过程中,需要回答若干问题,系统需要安装无数个软件,创建和写入无数的文件.因为涉及到大量的文件定位和读写,速度一定是快不起来的. ...

  7. 第二百五十八天 how can I 坚持

    装虚拟机了.CentOs,32位,这电脑装4台能不能带起来啊. 早上,流鼻血了,不知道咋回事.太干了... 明天得早起会,得利索着点,不能托托拉拉的. 还有,今天又忘带钥匙了.悲剧. 睡觉.hadoo ...

  8. 03 javadoc

    javadoc从程序源代码中抽取类.方法.成员等注释形成一个和源代码配套的API帮助文档 1.标签.命令格式: 2.使用方式: 2.1 dos命令行格式:javadoc XXX.java 2.2 ec ...

  9. Azure 公网及内网ip绑定方法

    此文章为我的云服务绑定情况,仅供参考,适用于已经创建vm,但开始未绑定vip,后期进行vip的绑定,注意:绑定ip会造成虚拟机暂时离线. -Location(vm所在地) 注意区分 北部和东部: Ch ...

  10. 汇总:Linux下svn命令大全

    svn(subversion)是近年来崛起的版本管理工具,svn服务器有2种运行方式:独立服务器和借助apache.2种方式各有利弊.不管是那种方式,都需要使用各种命令来实现.在本文中,haohtml ...