MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

PDO – PHP database extension

DevOpsSchool banner with PHP PDO logo and title PDO – PHP database extension

PDO – PHP database extension
PDO (PHP Data Objects) is a PHP extension through which we can access and work with databases. Though PDO is similar in many aspects to mySQLi, it is better to work with for the following reasons:

  • It is better protected against hackers.
  • It is consistent across databases, so it can work with MySQL as well as other types of databases (SQLite, Oracle, PostgreSQL, etc.)
  • It is object oriented at its core.

In this PDO tutorial you will find recipes for 4 basic functions that we perform with the database: insertion, selection, update, and deletion. The recipes are intended to work with MySQL, but we can easily switch it with another database.

How to connect with the database through PDO?
It is considered good practice to wrap the database connection within a try-catch block so that, if anything goes wrong, an exception will be thrown. We can customize the error message but, in order to keep things simple, we’ll settle with the error message that PDO provides.
In order to connect to the database, we’ll need the database name, username, and password.

[code language=”php”]
// DB credentials.
define(‘DB_HOST’,’localhost’);
define(‘DB_USER’,’your user name’);
define(‘DB_PASS’,’your user password’);
define(‘DB_NAME’,’your database name’);
// Establish database connection.
try
{
$dbh = new PDO(“mysql:host=”.DB_HOST.”;dbname=”.DB_NAME,
DB_USER, DB_PASS,
array(PDO::MYSQL_ATTR_INIT_COMMAND => “SET NAMES ‘utf8′”));
}
catch (PDOException $e)
{
exit(“Error: ” . $e->getMessage());
}
[/code]

How to close the database connection?
PHP automatically closes the database connection but, if the need arises, we can deliberately close the connection with the following line of code:

[code language=”php”]
$dbh = null;
[/code]

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
MotoShare.in is your go-to platform for adventure and exploration. Rent premium bikes for epic journeys or simple scooters for your daily errands—all with the MotoShare.in advantage of affordability and ease.

Related Posts

Introduction to PHP Data Object PDO in PHP

There are three main API options when considering connecting to a MySQL database server. PHP’s MySQL Extension. PHP’s mysqli Extension. PHP Data Object (PDO). PHP Data Object…

Read More

Complete reference guide of PHP Databases!

PHP MySQL Introduction MySQL is one of the most popular relational database system being used on the Web today. It is freely available and easy to install, however if…

Read More

How to delete records?

1) Write the delete statement: [code language=”php”] $sql = "DELETE FROM `users` WHERE `id`=:id"; [/code] 2) Prepare the query: [code language=”php”] $query = $dbh -> prepare($sql); [/code]…

Read More

How to use PDO to update the database?

1) Write the regular update statement and again, instead of values, assign the named placeholders. For example: [code language=”php”] $sql = "UPDATE `users` SET `city`= :city, `phone`…

Read More

How to use PDO to read data from the database?

1) Write the regular select statement and again, instead of values, put named placeholders. For example: [code language=”php”] $sql = "SELECT * FROM users"; [/code] 2) Prepare…

Read More

How to use PDO to insert data into the database?

The SQL code for the users table: [code language=”sql”] CREATE TABLE IF NOT EXISTS users (id int(11) NOT NULL AUTO_INCREMENT, name varchar(60) DEFAULT NULL, phone varchar(12) DEFAULT…

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x