Skip to main content

Posts

Showing posts from January, 2019

Database handling in PHP

There are three method for handling database in PHP 1. MySQLi-  object-oriented 2. MySQLi -  procedural -oriented 3. PDO - PHP Database Objec t First create variable required $servername = "localhost"; $username = "root"; $password = "password"; $dbname = "sample_db"; Method 1 : -  MySQLi -  object-oriented Create connection : - $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) {     die("Connection failed: " . $conn->connect_error); } Fetch the records from table  : - $sql = "SELECT * FROM users"; $result = $conn->query($sql); get rows count  : - $count = $result->num_rows; iterate through records  : - while($row = $result->fetch_assoc()) { //print single row details print_r($row); } close connection  : - $conn->close(); Method 2 :-  MySQLi -  procedural -oriented Create connection $conn = mysqli_con

.htaccess file

What is .htaccess file? A .htaccess file is an ASCII file. It is a short form of  Hypertext Access. It is a configuration file used by Apache-based web servers . The  .htaccess is the file’s extension. It is not a file  name.  It will affect on the folder  in which it is located and also on subfolders. we can use .htaccess for following Remove file extension Redirect to custom error pages Block or allow IP addresses Prevent directory listings How to remove .php extension from URL? Create .htaccess file in root of your project and add following code in it RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^(.*)$ $1.php Before  URL is like www.example.com/home.php After adding .htaccess it will be like www.example.com/home Redirect to custom error pages It is used to redirect the user to a custom error page by using the error code. Following are error codes 400 – Bad request 401 – Authorization Required 403 – Forb

AJAX and JSON

JSON – Javascript  Serialized   Object Notation JSON has key-value pair Following is the format of JSON {name: "Nilesh", age: 31, city: "Kolhapur"} We will submit data using ajax and get a response as JSON $.ajax({      type:'POST',        url:'submit.php',      data:{ name: "Abc" },      dataType: 'json',      success:function(data){         console.log( data.name );      },      error:function(data) {          alert(data);      }  });  POST method $.post( "submit.php", { name: "Abc" },  function( data ) {       console.log( data.name );  }, "json");  Following is shortform $.getJSON( "submit.php", function( data ) {      console.log( data.name );  });

Form submit using AJAX

AJAX = Asynchronous JavaScript And XML     AJAX is a client-side script which is used to communicate to the server.  It is used to get or submit the data or update the part of the webpage without refresh the page.  When we want to submit a form, first we get the form data using serialize method following code:- var datastring = $("#create-from").serialize(); If want to send custom data then, var datastring = {name:"ABC",email:"abc@gmail.com"} Now we will submit the form data using ajax $.ajax({      type:'POST',      url:'asySubmit.php',      data:datastring,      beforeSend: function(){                         $('.loader').show();      },      success:function(response) {              console.log(response);      },       error:function(response) {            alert(response);      }  }); If the form contains file input then use following code:- $.ajax({     type:'POST',     url:'asy

Date Time in PHP

     Initialize the object $date = new DateTime(); We can get timezone using following method $date->getTimezone(); We can set timezone using following method $date->setTimezone(new DateTimeZone('Asia/Calcutta')); Format of date $date->format('d-m-Y H:i:s'); Timestamp $date->getTimestamp(); $date->setTimestamp(1544972289); $date->format('d-m-Y H:i:s'); Interval //get date after 10 days and 2 hours $date->add(new DateInterval('P10DT2H')); //get date before 10 days and 2 hours $date->sub(new DateInterval('P10DT2H')); // after one month $date->modify('+1 month'); Difference between dates $birthdate = new DateTime("2000-01-01"); $diff=$birthdate->diff($date); echo $diff->y." years, ".$diff->m." months, ".$diff->d." days";