Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,444 members, 7,816,022 topics. Date: Thursday, 02 May 2024 at 11:42 PM

PHP Mysql Connection Using PDO - Webmasters - Nairaland

Nairaland Forum / Science/Technology / Webmasters / PHP Mysql Connection Using PDO (309 Views)

Learn PHP(OOP) And Mysql With PDO By Building A P2P Donation Website / Create A Peer To Peer(p2p) Website In PHP + Mysql / Fix/debug PHP & Mysql Issues On Your Website (2) (3) (4)

(1) (Reply)

PHP Mysql Connection Using PDO by skptricks: 4:22pm On Oct 19, 2017
Today, In this post we will learn how to connect to MySQL using PDO in PHP and just for the information PDO stands for "PHP Data Objects".
Here we will see the some useful examples to perform CURD operations in MySQL database using PDO and also it provides you some information about MySQL Prepared Statement.

Connections to MySQL database can be established by creating instances of the PDO base class and specify the driver, database name, username, and password.
SYNTAX:
$dbConnection = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);

Connection to MySQL Databse Using PDO

Lets see the simple example to establish the database connection to MySQL using PDO.
<?php
$dbhost ="localhost"; // set the hostname
$dbname ="skptricksdemo" ; // set the database name
$dbuser ="root" ; // set the mysql username
$dbpass =""; // set the mysql password


try {
$dbConnection = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$dbConnection->exec("set names utf8"wink;
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}

?>

OUTPUT:
----------------------
Connected successfully

Learn more by following below link :
http://www.skptricks.com/2017/10/connections-and-connection-management-using-pdo-mysql.html

(1) (Reply)

Get 50% Off Every Of You Web Hosting With Domainking Now / Forex Traders Wanted / Bloggers Squad - How To Write Unique Post And Monetize Your Blog

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 10
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.