Skip to main content

Posts

Showing posts from November, 2016

Beginner guide to Mongodb database

Beginner guide to Mongodb database MongoDB is an open-source document database that provides high performance, high availability, and automatic SCALING . MongoDB documents are similar to JSON objects. MongoDB stores data in the form of BSON -Binary encoded JSON documents which supports a rich collection of types. Fields in BSON documents may hold arrays of values or embedded document s. Structural aspects of MongoDB 1. Data Model A record in MongoDB is a document, which is a data structure composed of field and value pairs. MongoDB stores documents in collections.Collections are analogous to tables in relational databases.  Documents stored in a collection must have a unique _id field that acts as a primary key.  There are two ways to stores documents in a collection either in Normalized for or embedded into another document itself. a) Normalized Data Models The relationships between data is stored by links (references) from one document to another.       b) Embe

Create PHP PDO wrapper class

Create PHP PDO wrapper class Step 1: Create Config file config.php <?php ini_set("display_errors", 1);   define('DB_HOST', "localhost"); define('DB_NAME', "pdoTest"); define('DB_USER', "root"); define('DB_PASS', "root"); ?> Step 2: Now create wrapper class Database.php <?php class Database {     private $conn; private $stmt;     function __construct (){  include_once 'config.php'; try{ $this->conn = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME."", DB_USER, DB_PASS);  $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); return $this->conn; }catch(PDOException $e){             return $e->getMessage();         } } public function query ($query){ $query = trim($query); try { $this->stmt = $this->conn->prepare($query); return $this; }catch (PDO

MySql Trigger

MySql Trigger A trigger is a stored program which executed automatically to respond to a specific event. such as insert, update or delete occurred in a table. Syntax : CREATE     TRIGGER `event_name` BEFORE/AFTER INSERT/UPDATE/DELETE     ON `database`.`table`     FOR EACH ROW BEGIN -- trigger body -- this code is applied to every  -- inserted/updated/deleted row     END; Notes: event_name :  All triggers must have unique names within a schema trigger_event : Indicates the kind of operation that activates the trigger. tbl_name : The trigger becomes associated with the table named tbl_name Example 1: Suppose you have user table and user_audit table and you want to track new users only. Then you have to create a trigger DELIMITER $$ CREATE TRIGGER `blog_after_insert` AFTER INSERT  ON `user`  FOR EACH ROW  BEGIN SET @changetype = 'NEW'; INSERT INTO user_audit (user_id, changetype) VALUES (NEW.id, @changetype);     END $$ DE