postheadericon PHP insert update, delete using PDO part-1

Part-1: Configure and Setup the MySQL database connection using PDO.

In this example, I am going to create separate files for each database operations which require to do database connection. In part one, I define few constant that is going to use throw-out the application in config.php and conn.php where I define the $conn connection object.

config.php


<?php
define("servername","localhost");
define("username","root");
define("password","admin");
define("mydb","bsrdb");
?>



1. servername = Domain name
2. username = Database User Name
3. password = Database Password
4. mydb = Database Name

These constants are use in our connection file that is conn.php

conn.php

<?php
require("config.php");
try
{
$conn = new PDO("mysql:host=".servername.";dbname=".mydb, username, password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
catch(PDOException $e)
    {
echo "Connection failed: " . $e->getMessage();
    }
?>

Here in connection file I create the $conn connection object using PDO. In second step we set the error mode on PDO for debugging environment.


This example is divided in multiple part.


Part-1: Configure and Setup the MySQL database connection using PDO.
Part-2: Create a MySQL Database and a table using PDO.
Part-3: Insert record using prepared statement in PDO.
Part-4: Display Records using prepared statement.
Part-5: Search records using AJAX and PDO in PHP.
Part-6: Update Record using prepared statement in PDO.
Part-7: Delete Record using prepared statement in PDO.


0 comments:

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.