Posts

Showing posts from January, 2024

Insert Multiple Records in Mysqli Database

  <?php $servername = "localhost" ; $username = "root" ; $password = "" ; $dbname = "heer" ; //  Create Connection $conn = new mysqli ( $servername , $username , $password , $dbname ); //  Check Connection if ( $conn -> connect_error ) {     die ( "Connection Failed:" . $conn -> connect_error ); } echo "Connection Successfully" . "<br>" ; // $sql = 'INSERT INTO student(id, name, age, gender, phone, city) //         VALUES(3, "Chirag", 44, "Male", "9855632210", "Mumbai")'; // $sql .= ';INSERT INTO student(id, name, age, gender, phone, city) //         VALUES(4, "Aadit", 29, "Male", "8544632217", "Pune")'; // $sql .= ';INSERT INTO student(id, name, age, gender, phone, city) //         VALUES(7, "Manish", 33, "Male", "5499632207", "Mumbai")'; // $sql .=...

Mysqli database data display using Order By

  <?php $servername = "localhost" ; $username = "root" ; $password = "" ; $dbname = "heer" ; //  Create Connection $conn = new mysqli ( $servername , $username , $password , $dbname ); //  Check Connection if ( $conn -> connect_error ) {     die ( "Connection Failed" . $conn -> connect_error ); } echo "Connection Successfully" . "<br>" ; $sql = " SELECT id, name , age, gender, phone, city FROM student ORDER BY name " ; $result = $conn -> query ( $sql ); if ( $result -> num_rows > 0 ) {     while ( $row = $result -> fetch_assoc ())     {         echo "Id: " . $row [ "id" ] . ",  Name: " . $row [ "name" ] . ",  Age: " . $row [ "age" ] . ",  Gender: "           . $row [ "gender" ] . ",  Phone: " . $row [ "phone" ] . ",  City: " . $row [ ...

Mysqli database Connection and Display Table Data from Database

  <?php $servername = "localhost" ; $username = "root" ; $password = "" ; $dbname = "heer" ; $conn = new mysqli ( $servername , $username , $password , $dbname ); if ( $conn -> connect_error ) {     die ( "Connection Failed : " . $conn -> connect_error ); } $sql = " SELECT * FROM student " ; $result = $conn -> query ( $sql ); if ( $result -> num_rows > 0 ) {     while ( $row = $result -> fetch_assoc ())     {         echo "<pre>" ;         echo "Id: { $row ["id"]} - Name: { $row ["name"]} - Age: { $row ["age"]} - Phone No: { $row ["phone"]} - City: { $row ["city"]}" ;         echo "</pre>" ;     } } else {     echo "No result fount" ; } $conn -> close (); //  OUTPUT: //Id: 0 - Name: Raj - Age: 25 - Phone No: 9854233621 - City: Ahmedabad // Id: 1 - Name: Amit - Age: 22 - Phone No: 8533...

Get Functions

  <?php //There are many Get Functions in PHP as shown below: //get_class, get_parent_class, get_class_methods, get_class_vars, get_object_vars, get_called_class, //get_declared_classes, get_declared_interfaces, get_declared_traits, class_alias class ParentClass {     function name ()     {         echo "Class Name : " . get_class ( $this ) . "<br>" ;   //Print the particualr Class Name         //Print the particular Parent Class Name as shown below:         echo "Parent Class Name : " . get_parent_class ( $this ) . "<br>" ;     } } class ChildClass extends ParentClass {     function __construct ()     {}     function myfunction ()     {}     function myfunction2 ()     {} } $obj = new ChildClass (); $obj -> name (); echo "Class Name is : " . get_class ( $obj ) . "<br>" ; echo "Paren...

Conditional Functions

  <?php //All Conditional Functions will give two result : True OR False // class_exists(), interface_exists(), method_exists(), trait_exists(), property_exists(), is_a(), //  is_subclass_of() are Conditional Functions in PHP class MyClass {       public $test ;     public function myMethod ()     {} } $object = new MyClass (); interface MyInterface {} trait myTrait {} //  Checking Whether any Object is of any particular Class OR Not if ( is_a ( $object , "MyClass" )) {     echo "This Object is of Class MyClass" . "<br>" ; } else {     echo "This Object is Not of Class MyClass" . "<br>" ; } //  Checking for the Existence of Property if ( property_exists ( "MyClass" , "test" )) {     echo "This property is Exists" . "<br>" ; } else {     echo "This property is Not Exists" . "<br>" ; } //  Checking for the Existence of Trait if ( tr...

Wakeup Method

<?php //serialize() function will convert Object to Array. We pass Object to serialize() function. //unserialize() function will convert Array to Object. We pass Array to unserialize() function. //When we use serialize() function at that time __sleep() method will automatically Call. //When we use unserialize() funtion at that time __wakeup() method will automatically Call. //__wakeup() is mostly used while making connection with database operations. class Student {     public $course = "PHP" ;     private $first_name ;     private $last_name ;     public function setName ( $fname , $lname )     {         $this -> first_name = $fname ;         $this -> last_name = $lname ;     }     public function __sleep ()     {         return array ( "first_name" , "last_name" );     }     public function __wakeup () ...

Sleep Method

<?php //serialize() function is used to Convert any Object into Array. //__sleep() function will be executed before serialize(). //When we serialize any Object at that time __sleep() Magic function will automatically Call //before serialize(). //serialize() function will convert Object to Array. We pass Object to serialize() function. //unserialize() function will convert Array to Object. We pass Array to unserialize() function. //When we use serialize() function at that time __sleep() method will automatically Call. //When we use unserialize() funtion at that time __wakeup() method will automatically Call. class Student {     public $course = "PHP" ;     private $first_name ;     private $last_name ;     public function setName ( $fname , $lname )     {         $this -> first_name = $fname ;         $this -> last_name = $lname ;     }     public function __s...

toString Method

  <?php //__toString() is a Magic Method. //When we try to print any Class's Object as a String at that time __toString() method will //automatically Call. //__toString() function will automatically call when we try to print any Object(of Class) as a String. class Abc {     public function __toString ()     {         return "Can't print Object as a String of Class : " . get_class ( $this );     } } $test = new Abc (); echo $test ; //  OUTPUT: //Can't print Object as a String of Class : Abc ? >

Unset Method

  <?php //When we try to unset any class's private property from outside the class at that time //__unset() method will automatically Call. class Student {     public $course = "PHP" ;     private $first_name ;     private $last_name ;     public function setName ( $fname , $lname )     {         $this -> first_name = $fname ;         $this -> last_name = $lname ;     }     public function __unset ( $property )     {         unset ( $this -> $property );     } } $test = new Student (); $test -> setName ( "Yahoo" , "Baba" ); echo "<pre>" ; print_r ( $test ); echo "</pre>" ; echo "<br>" ; unset ( $test -> first_name ); echo "<pre>" ; print_r ( $test ); echo "</pre>" ; unset ( $test -> course ); echo "<pre>" ; print_r ( $test ); echo "</pre>" ; // echo $te...