Before we use the PDO, we will know why we will use PDO.
PDO Only supports object-oriented.It has many advantages and disadvantages.But the benedits are more.
If we compare with Mysqli, I will see then Mysql only supports Mysql Database. But On the other hand PDO can supports many Databases.If we want to use any others database with PHP then we must work PDO.
Another advantage of the PDO, Suppose you are using Mysql Databas but after some time I thought I would use Oracel database. In This case, The PDO can be easily accessed from others database.
<?php
class Database
{
private static $dbname='databasename';
private static $dbhost='localhost';
private static $dbusername='user';
private static $dbuserpassword='password';
private static $cont= null;
public function __construct(){
die('init function not access');
}
public static function connect(){
if(null== self::$cont)
{
try
{
self::$cont= new PDO("mysql:host=".self::$dbhost.";" ."dbname=".self::$dbname,
self::$dbusername,self::$dbuserpassword);
}
catch (PDOException $e)
{
die($e->getMessage());
}
}
return self::$cont;
}
public static function disconnect()
{
self::$cont=null;
}
}
?>
See Example
<?php
$pdo= Database::connect();
$sql="select * from table where ID=:UID";
$data= $pdo->prepare($sql);
$data->execute(array(':UID' => $_SESSION['ID']));
$post = $data->fetch();
if($post){
foreach($post as $postdata ){ ?>
<h1><?php echo $postdata['username'] ; ?></h1>
<?php
}
}
?>
Today we will learn how to verify email id while signup. it's very important for the real user. You ..
This is a common problem for those of you who are new to coding. While coding we often make some syn..
Warning: session start() [function.session-start]: Cannot send session cache limiter - headers alrea..
Before we use the PDO, we will know why we will use PDO.PDO Only supports object-oriented.It has man..
Deprecated: autoload() is deprecated, use spl autoload register() instead. How to replace this (my a..
getimagesize() is a PHP function that returns an array containing information about the size and typ..
Today we will know - How to login with otp in php. Here we will use php,mysql & ajax. Before tha..
Get the latest news and updates by signing up to our daily newsletter.We won't sell your email or spam you !