Posts

Showing posts from June, 2024

DATE Functions tutorial in PHP

If we want to get current date then we can use below shown sql query : SELECT CURRENT_DATE();               OR       We can use SELECT CURDATE(); If we want to get current date with time then we can use below sql query : SELECT SYSDATE(); we can also use  SELECT NOW(); If we want to find out month from sql query then we can use below shown sql query : SELECT MONTH("2019-10-15 09:34:21") AS DATE;

DROP and TRUNCATE Table Tutorial in PHP

 DROP will remove Whole Table(including Structure of Table with data) and TRUNCATE will remove only Whole data of table but table structure will remain as it is. This is the major difference between DROP and TRUNCATE. DROP and TRUNCATE Syntax : DROP TABLE table_name; TRUNCATE TABLE table_name;

ALTER Command Tutorial in PHP

Features of MySQL ALTER Command : 1) Add Column in a Table 2) Changing Data Type of a Column 3) Change Column Name 4) Adding Constraints to a Column 5) Changing Column Position 6) Delete Column 7) Renaming Tables ALTER Syntax : 1) For Add Column : ALTER TABLE table_name ADD column_name datatype; 2) For Modify Column : ALTER TABLE table_name MODIFY column_name datatype; 3) For Delete Column : ALTER TABLE table_name DROP COLUMN column_name datatype; 4) For Rename Column : ALTER TABLE table_name CHANGE column_name New_name datatype; 5) For Rename Table : ALTER TABLE table_name RENAME new_table_name; If we want to Add Extra Column named "Email" in table named "students" then we can use below shown sql query : ALTER TABLE students ADD Email varchar(100); If we want to place "Email" column after "name" column then in "students" table then we can use below shown sql query : ALTER TABLE students MODIFY Email varchar(100) AFTER name; If we want ...

UNION and UNION ALL in PHP

 In "personal" table, there are 6 columns named id, name, percentage, age, gender, city. In "personal" table, column name "id" is PRIMARY KEY and column name "city" is FOREIGN KEY. In "courses" table, there are 2 columns named course_id and course_name. In "courses" table, column name "course_id" is PRIMARY KEY. In "city" table, there are 2 columns named cid and cityname. In "city" table, column name "cid" is PRIMARY KEY. In "lecturers" table, there are 6 columns named id, name, percentage, age, gender, id_of_personal. In "lecturers" table, column name "id" is PRIMARY KEY and column name "id_of_personal" is FOREIGN KEY. UNION and UNION ALL Syntax : SELECT column1, column2 FROM table1 UNION/UNION ALL SELECT column1, column2 FROM table2; RULES : 1) Each SELECT statement within UNION must have the same number of columns. 2) The columns must also have ...

SubQuery with EXISTS and NOT EXISTS in PHP

 SELECT with SubQuery Syntax : SELECT columns FROM table1 WHERE column = (SELECT columns FROM table2 WHERE condition); In "personal" table, there are 6 columns named id, name, percentage, age, gender, city. In "personal" table, column name "id" is PRIMARY KEY and column name "city" is FOREIGN KEY. In "courses" table, there are 2 columns named course_id and course_name. In "courses" table, column name "course_id" is PRIMARY KEY. In "city" table, there are 2 columns named cid and cityname. In "city" table, column name "cid" is PRIMARY KEY. If we want to select records from "personal" table who belong to Delhi from "city" table using SubQuery then we can use below shown sql query : SELECT name FROM personal WHERE city = (SELECT cid FROM city WHERE cityname = "Delhi"); If we want to select records from "personal" table who belong to Delhi and Agra from...

GROUP BY Clause and HAVING Clause in PHP

GROUP BY :  The GROUP BY clause is used in conjunction with the SELECT Statement and Aggregate functions to group rows together by common column values. SELECT with GROUP BY Syntax : SELECT columns FROM table_name WHERE condition GROUP BY column_name(s); (Here above "condition" is Optional in above query). In "personal" table, there are 6 columns named id, name, percentage, age, gender, city. In "personal" table, column name "id" is PRIMARY KEY and column name "city" is FOREIGN KEY. In "courses" table, there are 2 columns named course_id and course_name. In "courses" table, column name "course_id" is PRIMARY KEY. In "city" table, there are 2 columns named cid and cityname. In "city" table, column name "cid" is PRIMARY KEY. SELECT with GROUP BY with Two Tables Syntax : SELECT columns FROM table1 INNER JOIN table2 ON table1.column_name = table2.column_name WHERE condition GROUP B...

JOIN Multiple Tables in PHP

 If we want to join multiple tables and table names are "personal", "city" and "courses" then we can use below shown sql query :  In "personal" table, there are 6 columns named id, name, percentage, age, gender, city. In "personal" table, column name "id" is PRIMARY KEY and column name "city" is FOREIGN KEY. In "courses" table, there are 2 columns named course_id and course_name. In "courses" table, column name "course_id" is PRIMARY KEY. In "city" table, there are 2 columns named cid and cityname. In "city" table, column name "cid" is PRIMARY KEY. If we want to join above three tables then we can use below shown sql query : SELECT * FROM personal INNER JOIN city ON personal.city = city.cid INNER JOIN courses ON personal.city = courses.course_id; If we want to apply alias or short name for table name then we can use below shown sql query : SELECT * FROM pers...

INSERT DATA in TABLE with SQL Command in PHP

 If we want to add data in table named "personal" then we can use below shown sql query : INSERT INTO personal(id,name,percentage,age,gender,city)  VALUES(1,"Ram Kumar",45,19,"M",1),  (2,"Sarita Kumari",55,22,"F",2),  (3,"Salman Khan",62,20,"M",2),  (4,"Juhi Chawla",47,18,"F",3),  (5,"Anil Kapoor",74,22,"M",1),  (6,"John Abraham",64,21,"M",2),  (7,"Shahid Kapoor",52,20,"M",1);

CREATE TABLE with SQL Command with PRIMARY KEY and FOREIGN KEY

 If we want to create table named "personal" with PRIMARY KEY and FOREIGN KEY then we can use below shown sql query : CREATE TABLE personal( id INT NOT NULL,  name VARCHAR(50) NOT NULL,  percentage INT NOT NULL,  age INT NOT NULL,  gender VARCHAR(1) NOT NULL,  city INT NOT NULL,  PRIMARY KEY(id),  FOREIGN KEY(city) REFERENCES city(cid) );

Traits Introduction in PHP OOPS

  <?php // When we want to use some property(of Outside Class) in One class but not in Second // and Third class at that time we can use trait functionality as shown below : //For Example, if we want to use some functions in one class but not in second class and //third class then we can make trait and we can use multiple trait in single //class as shown below: //NOTE : we can not inherit multiple class by single class in case of Inheritance but we //can use multiple trait in single class. This is the difference between Inheritance and //trait as shown below example. class Abc {     public function test () {         echo "Test from class ABC" ;     } } trait test {     public function test1 () {         echo "test1 method of test1 trait <br>" ;     } } trait test2 {     public function test2 () {         echo "test2 method of test2 trait <br>" ...

Polymorphism in PHP OOPS

  <?php //Polymorphism means many types or many forms // Polymorphism spl_autoload_register ( function ( $class ) {     include "classes/ $class .php" ; }); function getLogger ( $type ) {     switch ( $type ) {         case "email" :         return new EmailLogger ();         break ;         case "database" :         return new DBLogger ();         break ;         case "file" :         return new FileLogger ();         break ;     } } $logger = getLogger ( "email" ); $profile = new UserProfile ( $logger ); $profile -> createUser (); ? > <?php class DBLogger implements LoggerInterface {     public function log ( $message ) {         echo "Logging message to DB: $message " ;     } } ? > <?php clas...

Dependency Injection in PHP OOPS

  <?php //  Dependency Injection class Logger {     public function log ( $message )     {         echo "Logging Message : $message " . "<br>" ;     } } $logger = new Logger (); $logger -> log ( "This is a message" ); //Here below, "UserProfile" class is become dependent on "Logger" class. class UserProfile {     private $logger ;     public function createUser ()     {         /// create user.         $this -> logger -> log ( "User created." );     }     public function updateUser ()     {         /// update user.         $this -> logger -> log ( "User updated." );     }     public function deleteUser ()     {         /// delete user.         $this -> logger -> log ( "User de...

Late Static Binding in PHP OOPS

  <?php class Db {     protected static $table = "baseTable" ;     public function select () {         echo " SELECT * FROM " . static :: $table . "<br>" ;     }     public function insert () {         echo " INSERT INTO " . static :: $table . "<br>" ;     } } class Abc extends Db {     protected static $table = "abc" ; } class UserAccounts extends Db {     protected static $table = "user_accounts" ; } $abc = new Abc (); $abc -> select (); $abc -> insert (); $accounts = new UserAccounts (); $accounts -> select (); $accounts -> insert (); // OUTPUT : //SELECT * FROM abc // INSERT INTO abc // SELECT * FROM user_accounts // INSERT INTO user_accounts ? >

Static Members in PHP OOPS

<?php //  Example 1 class Abc {     public static $data = "test data" ;     public static $size = "big data here" ;     public static function xyz () {         echo "xyz function <br>" ;     }     public static function getSize () {         return self :: $size ;     }     public static function setSize ( $s ) {         return self :: $size = $s ;     } } echo Abc :: $data . "<br>" ; Abc :: xyz () . "<br>" ; echo Abc :: getSize () . "<br>" ; echo Abc :: setSize ( 1200 ) . "<br><br><br>" ; //  Example 2 class AbcTwo {     public static $objectCount = 0 ;         public static function getCount () {         return "Total Objects created by class are : " . self :: $objectCount ;         ech...

Interface in PHP OOPS

  <?php //We can not use member variables or data memebers in interface. //we can not make constructor function in interface. //we can not use private or protected methods in interface. interface a {     public function abc (); } interface b {     public function xyz (); } class C implements a , b {     public function abc ()     {         echo "abc function called . <br>" ;     }     public function xyz ()     {         echo "xyz function called . <br>" ;     } } $testObject = new C (); $testObject -> abc () . "<br>" ; $testObject -> xyz () . "<br>" ; // OUTPUT : //abc function called . // xyz function called . ? >

Abstract Classes in PHP OOPS

  <?php abstract class BaseEmployee {     protected $firstname ;     protected $lastname ;     public function getFullName ()     {         return $this -> firstname . " " . $this -> lastname ;     }         public abstract function getMonthlySalary ();     public function __construct ( $f , $l )     {         $this -> firstname = $f ;         $this -> lastname = $l ;     } } class FullTimeEmployee extends BaseEmployee {     protected $annualSalary ;         public function getMonthlySalary ()     {         return $this -> annualSalary / 12 ;     } } class ContractEmployee extends BaseEmployee {     protected $hourlyRate ;     protected $totalHours ;     public function getMon...

Encapsulation in PHP OOPS

<?php //After creating Object, only public property can be accessed,not private and not protected. class TV {     protected $model ;     private $volume ;     public function volumeUp ()     {         $this -> volume ++;     }     protected function getModel ()     {         return $this -> model ;     }     public function volumeDown ()     {         $this -> volume --;     }     public function __construct ( $m , $v )     {         $this -> model = $m ;         $this -> volume = $v ;     } } class Plazma extends TV {     public function getModel ()     {         return $this -> model ;     } } $tv = new Plazma ( "abc" , 1 ); echo $tv -> getModel (); // O...

Inheritance in PHP OOPS

<?php class TV {     public $model ;     public $volume ;     public function volumeUp ()     {         $this -> volume ++;     }         public function volumeDown ()     {         $this -> volume --;     }         //Here Below constructor(of class TV) will be override because there is also a     // constructor(of class TvWithTimer) present inside TvWithTimer Class as shown below :     public function __construct ( $m , $n )     {         $this -> model = $m ;         $this -> volume = $n ;     } } class TvWithTimer extends TV {     public $timer = true ;     public function __construct ( $a , $b )     {         echo "Hello $a , this is TvWithTimer class constructor and value i...

Classes and Objects in PHP OOPS

<?php class TV {     public $model = "xyz" ;     public $volume = 1 ;         //By default functions are public unless you define private or protected.     public function volumeUp ()     {         $this -> volume ++;     }     public function volumeDown ()     {         $this -> volume --;     } } $tv_one = new TV (); $tv_two = new TV (); echo $tv_one -> volume . "<br>" ; echo $tv_two -> volume . "<br>" ; echo $tv_one -> model . "<br>" ; echo $tv_two -> model . "<br>" ; $tv_one -> volumeUp (); echo $tv_one -> volume . "<br>" ; $tv_one -> volumeDown (); echo $tv_one -> volume . "<br>" ; // OUTPUT : //1 // 1 // xyz // xyz // 2 // 1 ? >