<?php var_dump(PDO::getAvailableDrivers()); ?>
<?php /* PDO, by using PDO class. Here we are creating $pdo object of PDO class and passing arguments to constructor method. */ $pdo = new PDO("mysql:host=localhost;dbname=db", 'username', 'password'); // mysqli, procedural way $mysqli = mysqli_connect('localhost','username','password','db'); // mysqli, object oriented way $mysqli = new mysqli('localhost','username','password','db'); ?>
<?php $db = new PDO('mysql:host=localhost;dbname=database', 'root', ''); // insertion with pdo using prepared statement start // storing user form input data in an array named as data $data = [ 'first' => 'Rahul', 'last' => 'Yadav' ]; // named placeholders $statement = $db->prepare("insert into users (firstname, lastname) values (:first, :last)"); if ($statement->execute($data)) { echo "Records inserted successfully"; } else { echo "Unsuccessfull"; } // insertion with pdo using prepared statement ends ?>
<?php $db = new PDO('mysql:host=localhost;dbname=database', 'root', ''); // insertion with pdo using prepared statement start // storing user form input data in an array named as data $data = [ 'Rahul', 'Yadav' ]; // unnamed placeholders $statement = $db->prepare("insert into users (firstname, lastname) values (?, ?)"); if ($statement->execute($data)) { echo "Records inserted successfully"; } else { echo "Unsuccessfull"; } // insertion with pdo using prepared statement ends ?>
<?php $db = new PDO('mysql:host=localhost;dbname=database', 'root', ''); // update with pdo using prepared statement start // storing user form input in an array named as params $params = [ 'first' => 'R', 'last' => 'Y', 'user_id' => '1' ]; $statement = $db->prepare("update users set firstname = :first, lastname = :last where id = :user_id "); if ($statement->execute($params)) { echo "Records updated successfully"; } else { echo "Unsuccessfull"; } // update with pdo using prepared statement ends ?>
<?php $db = new PDO('mysql:host=localhost;dbname=database', 'root', ''); // deletion with pdo using prepared statement start // data we want to delete in an array named as params $params = [ 'first' => 'R', 'last' => 'Y' ]; $statement = $db->prepare("delete from users where firstname = :first and lastname = :last"); if ($statement->execute($params)) { echo "Records deleted successfully"; } else { echo "Unsuccessfull"; } // deletion with pdo using prepared statement ends ?>
Technical Architect
Kuala Lumpur, Malaysia
I'm a Full Stack Web Developer working in a MNC and passionate of developing modern web and mobile applications.
I have designed and developed CodephpOnline & CodephpOnline Wiki platform to expatiate my coding and technology learning experiences.
In my leisure time, I write technical articles on web development such as PHP, Laravel, CodeIgniter, Mediawiki, Linux, Angular, Ionic, ReactJS, NodeJS, AJAX, jQuery, Cloud and more.