Skip to main content

Posts

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

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, change...

CakePHP Interview Question and Answer

CakePHP Interview Question and Answer What is the first file that gets loaded when you run a application using cakephp? Answer: bootstrap.php     You can be changed it either through index.php , or through .htaccess     How to change via webroot > index.php         if (!defined('CAKE_CORE_INCLUDE_PATH')) {         if (function_exists('ini_set')) {             ini_set('include_path', ROOT . DS . 'lib' . PATH_SEPARATOR . ini_get('include_path'));         }         if (!include('Cake' . DS . 'bootstrap.php')) { // Change bootstrap.php file to any file name             $failed = true;         }     } else {         if (!include(CAKE_CORE_INCLUDE_PATH . DS . 'Cake' . DS . 'bootstrap.php')) {             $failed = true;     ...

Javascript Local Storage

Javascript Local Storage LocalStorage is used to stored web application data within the user's browser. LocalStorage is only store strings in the different keys How to check user's browser support? if (typeof(Storage) !== "undefined") {     // Code for localStorage/sessionStorage. } else {     // Sorry! No Web Storage support.. } How to Set Values in localStorage localStorage. setItem ('favoriteflavor','vanilla'); How to Access localStorage variable If you read out the favoriteflavor key, you will get back “vanilla”: var taste = localStorage. getItem ('favoriteflavor'); How to remove Elements from localStorage localStorage. removeItem ('favoriteflavor'); var taste = localStorage.getItem('favoriteflavor'); How to Store Array in LocalStorage localStorage is for key : value pairs, so what you'd probably want to do is JSON.stringify the array and store the string in the mycars key and then you can pul...

YouTube Player API Reference for iframe Embeds

YouTube Player API Reference for iframe Embeds The IFrame player API lets you embed a YouTube video player on your website and control the player using JavaScript.Using the API's JavaScript functions, you can queue videos for playback; play, pause, or stop those videos; adjust the player volume; or retrieve information about the video being played. <script>       // 1. This code loads the IFrame Player API code asynchronously.     var tag = document.createElement('script');     tag.src = "https://www.youtube.com/iframe_api";     var firstScriptTag = document.getElementsByTagName('script')[0];     firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);     // 2. This function creates an <iframe> (and YouTube player)     //    after the API code downloads.     var player;            f...