Skip to main content

Posts

Showing posts with the label PHP PDO

Create PHP PDO wrapper class

Create PHP PDO wrapper class Step 1: Create Config file config.php <?php ini_set("display_errors", 1);   define('DB_HOST', "localhost"); define('DB_NAME', "pdoTest"); define('DB_USER', "root"); define('DB_PASS', "root"); ?> Step 2: Now create wrapper class Database.php <?php class Database {     private $conn; private $stmt;     function __construct (){  include_once 'config.php'; try{ $this->conn = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME."", DB_USER, DB_PASS);  $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); return $this->conn; }catch(PDOException $e){             return $e->getMessage();         } } public function query ($query){ $query = trim($query); try { $this->stmt = $this->conn->prepare($query); return $this; }catch (PDO...

How to implement PDO in PHP

Introduction PHP PDO  Connecting to SQL: 1. new PDO("sqlsrv:Server=$servername; Database=$dbname",$username,$password); Connecting to Oracle:        1. new PDO("OCI:dbname=accounts;charset=UTF-8","username","password"); Connecting to PgSQL: 1. $db = new PDO("pgsql:dbname=pdo;host=localhost","username", "password"); Connecting to MySQL: 1. $db = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'username', 'password'); 2. $db = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'username', 'password', array(PDO::ATTR_EMULATE_PREPARES => false,                                         PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); 3. $db = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'username', 'password');           $db->setAttribute(PDO::ATTR_ERRMODE, PDO::E...