Friday 18 July 2014

What is PDO in Php?

PDO(PHP DATA OBJECTS)
PDO is  an object oriented class which allowed users to connect and execute the functions on various databases in an object oriented way.   By using PDO,we can get around SQL injection by using “Prepared Statements". The  Mysql secure  functions is less and older work , but with consequences.Using the function mysql_query to execute queries at the database level lead
to major security flaws commonly referred to as sql injection
Why PDO?
  • Support nice variety of info systems supported by PHP.
  • Your code less difficult to put in. don't want third party code.    you do not want editing of the many line code for every info. simply write one and run anyplace.
  • Speed: PDO has compiled language, PHP libraries (ADOdb, PEAR DB) written in associate taken language.
  • You need to enable the PDO and PDO_MYSQL extensions in your php.ini file.    
On a windows server you can add the following lines in your php.ini to enable the pdo
  • extension = php_pdo.dll
  • extension = php_pdo_mysql.dll 
PDO – connect and close 
# MySQL with PDO_MYSQL
$DBH = new PDO("mysql:host=$host_name;dbname=$dbname_name", $user_name, 
# close the db connection
$DBH = null;
PDO – exceptions
PDO can use exceptions to handle error, which means your code with PDO should be wrapped in a try/catch block to catch the errors.
we can use PDO with one of 3 error modes by setting the error mode attributes on your newly created database handle
  • $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT ); # default error mode
  • $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING ); # it is useful for debugging,program  continue to execute
  • $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); # most situations ,it fires an exception

PDO  create and update   

Using PDO, create and update is normally a two-step process 

Prepare -> Bind ->Execute 

  • We can executed prepared statement multiple times by just sending the data to the server because prepared statement is a precompiled statement.
  • It has the added advantages of automatically making the data used in the placeholder safe from SQL injection attacks.
# The most basic type of insert, STH means "Statement Handle", no binding here
$STH = $DBH->prepare("INSERT INTO folks ( first_name ) values ( 'Cathy' )");
$STH->execute();
<?php
# no placeholders - ripe for SQL Injection!

$STH = $DBH->prepare("INSERT INTO folks (name, addr, city) values ($name, $addr, $city)");
# unnamed placeholders
$STH = $DBH->prepare("INSERT INTO folks (name, addr, city) values (?, ?, ?)");
# named placeholders
$STH = $DBH->prepare("INSERT INTO folks (name, addr, city) value (:name, :addr, :city)");
?>

PDO - prepared statements 1
• Unnamed placeholders
$STH = $DBH->prepare("INSERT INTO folks (name, addr, city) values (?, ?, ?)");
# assign variables to each place holder, indexed 1-3
$STH->bindParam(1, $name); $STH->bindParam(2, $addr); $STH->bindParam(3, $city);
# insert one row - once the query have been prepared ...
$name = "Daniel";
$addr = "1 Wicked Way";
$city = "Arlington Heights";
$STH->execute();
PDO - prepared statements 2
• Named placeholders
$STH = $DBH->prepare("INSERT INTO folks (name, addr, city) value (:name, :addr, :city)");
# the first argument is the named placeholder name - notice named placeholders always start with a colon
$STH->bindParam(':name', $name); $STH->bindParam(':addr', $addr); $STH->bindParam(':city', $city);
# insert one row - insert as many rows as you want just updating the variables and ->execute()
$name = "Daniel"; $addr = "1 Wicked Way"; $city = "Arlington Heights";
$STH->execute();
PDO - prepared statements 3
• Update and delete with named placeholders
<?php
// update using named place holders
$id = 5;
$name = "Joe the Plumber";
try {
$DBH = new PDO('mysql:host=localhost;dbname=someDatabase', $username, $password);
$DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$STH = $DBH->prepare('UPDATE someTable SET name = :name WHERE id = :id');
$result = $STH->execute(array(':id' => $id, ':name' => $name));
echo $STH->rowCount(), " - ", $result;
}
catch(PDOException $e) {
echo 'Error: ' . $e->getMessage();
}/
/ delete using named place holders and the bindParam method
$id = 5;
try {
$DBH = new PDO('mysql:host=localhost;dbname=someDatabase', $username, $password);
$DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$STH = $DBH->prepare('DELETE FROM someTable WHERE id = :id');
$STH->bindParam(':id', $id);
$result = $STH->execute();
echo $STH->rowCount(), " - ", $result;
}
catch(PDOException $e) {
echo 'Error: ' . $e->getMessage();
}?
> 
 




Wednesday 16 July 2014

What's a content management system (CMS)?

  CMS ?

A content management system is software that keeps track of every piece of content on your Web site,just like your library keeps track of books and stores them in a arrainging manner. Content can be simple text, photos, music,  documents, video,graphics or just about anything  which you can deal it. A major advantage of using a CMS is that it requires almost no technical skill or knowledge to manage it. Since the CMS manages all your content, which  you have to.

What are some real world examples of what CMS! can do? CMS is used all over the world to power Web sites of all types, sizes and shapes. For example:
  • Corporate Web sites or portals
  • Corporate intranets and extranets
  • Online newspapers, magazines, Banners,and publications
  • E-commerce and online reservations
  • Personal or family homepages
  • Small business Web portals
  • Non-profit and organizational Web portals
  • Community-based  web-portals
  • School and church Web sites
  • Government  offical applications

Monday 14 July 2014

How to delete all the zip files in current directory in php


Know about file location

1.     We are on 'C:\xampp\htdocs\letdelete\index.php

               In letdelete directory we have some zipp files  ,we want to delete all the zip files.

v      Alice.zip 

v      Mone.zip

v      Cane.zip

index.zip
$ar = array();
$ar1 = array();
$directory = "./";
$files = glob($directory ."*");
foreach($files as $file)
{
if (is_dir($file)) {
/* nothing to do*/
}else {
$ar1[] =  basename($file);
}
}
$ab=count($ar1);
for($a=0;$a<$ab;$a++)

{
$source=$ar1[$a];
if (strpos($source,'zip') !== false) {
if(file_exists($source))
{
unlink($source);
}    }     }



How to send email in PHP

Before Starting...

Know about file location

1.Your index.php must be in server side.
Note :It is not working in localhost.
index.php
<?php
$to      = 'gauravrautela123@gmail.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: gauravrautela123@gmail.com' . "\r\n" . phpversion();
mail($to, $subject, $message, $headers);
?>

Terminology:
$headers : Receiver email  address,you can attach more than one email addresses.
$to : Sender email address.

 


Friday 11 July 2014

How to zip all directory/folders in root folder in php

Before Starting...

Know about file location

My File exist in C:\xampp\htdocs\ftp\index.php 
In ftp directory we have  6 more directories and  names are
  • John
  • Jill
  • Cafe
  • Monit
  • Rifle
  • Kim.
3. We Want to zip all the directory at one time without enter the  individual directory name.

index.php 
<?php
$ar = array();
$directory = "./";
$files = glob($directory . "*", GLOB_ONLYDIR );
foreach($files as $file)
{
   $ar[] =  basename($file);
}
$a =count($ar); 
for($i=0;$i<$a;$i++)
{
$filename= $ar[$i];
$source_path= $filename;
$the_folder =$source_path;
$zip_file_name = $the_folder.".zip";
$za = new FlxZipArchive;
$res = $za->open($zip_file_name, ZipArchive::CREATE);
if($res === TRUE) 
{
 $za->addDir($the_folder, basename($the_folder));
 $za->close();
}
else  { echo 'Could not create a zip archive';}
} ?>


How to get all directory name of root in php


Before Starting...

Know about file location

       My File exist in C:\xampp\htdocs\ftp\index.php
In ftp directory we have  6 more directories  names are :
  • John
  • Jill
  • Cafe
  • Monit
  • Rifle
  • Kim.
we want to know all the directory name by uisng index.php
<?php
$ar = array();
$directory = "./";
$files = glob($directory . "*", GLOB_ONLYDIR );
foreach($files as $file)
{

$ar[] =  basename($file);
}
 
$a =count($ar);
for($i=0;$i<$a;$i++)
{
echo $ar[$i]."<br>";
}  

Output Will be
John
Jill
Cafe
Monit
Rifle
Kim.



Thursday 10 July 2014

How to concatenate two string / multiple strings in php

First We learn how to concatenate two string.  

$firststring="Hello ";  
$secondstring="New World";  
$final =$firststring. $secondstring;
echo $final; 
Output will be:
 Hello New World

Second We learn how to concatenate two Numbers.
 $firstno= 123;
 $secondno=567;
  $final =$firstno.$secondno;

 echo $final;
 Output will be:
 123567

Third We learn how to concatenate more than two strings.
 $firststring="Hello ";
 $secondstring="New World";
 $thirdstring ="My World";
$final =$firststring. $secondstring."and this is".$thirdstring;
 echo $final;
 Output will be:
 Hello New World and this is My World