PHP Database Connect using PDO - Web Development and Web Design Codes

Latest

Friday, April 28, 2017

PHP Database Connect using PDO

How To Connect MySQL Database using PHP Data Objects(PDO)

PDO Database Connect
PDO Database Connect

Overview and Structure:

DataBase Connect is Main Factor to work with PHP MySQLi.Without DataBase Connection you Can't Access any Table In DataBase.You Can't Insert,Update,Delete,select Data and more.DataBase Connection is First Step to Learn PHP MySQL
What You Will Need:
Go To XAMPP=>htdocs=>create a new file connect.php
Now copy and Paste The Below Codes and paste it connect.php file

DataBase Structure and Overview:

Create a New DataBase "Tutorial"
DataBase Name: Tutorial.

Code For Connect.php:

<?php
$host = "localhost"; //Host Name
$username = "root"; //MySQL Username.if you don't know leave 'root' by Defaults
$password = ""; //MySQL Password if Password Protected .else Leave it Blank
$db_name = "tutorial"; //Database Name

try {
 //Creating Connection between Server and Database
    $conn = new PDO("mysql:host=$host;dbname=$db_name", $username, $password);
    // SET PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "You have successfully Connected"; 
    }
catch(PDOException $e)
    {
  //Displaying Error Message with Detail.
    echo "Sorry failed to Connect: " . $e->getMessage();
    }
?>
 

If You Like This Article Please Like and Share with Your Friends.
Don't Forget to Ask comments.Thank You.

No comments:

Post a Comment

Thank You for Your Comment

Note: Only a member of this blog may post a comment.