Open In App

How to connect the Database with PHP DOM page ?

Last Updated : 31 May, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see how to connect the database with the PHP DOM page?

Approach: Make sure you have XAMPP or WAMP installed on your machine. In this tutorial, we will be using the XAMPP server.

To connect the database with the PHP DOM page, follow the steps given below:

1. Create Database: First, we will create a database named ‘geeksforgeeks‘ (you can give any name to your database). You can also use your existing database or create a new one.

create database “geeksforgeeks”

2. Create Table: We now create a table named ‘adminlogin‘ with 3 columns to store the data.

create table “adminlogin”

3. Create Table structure: The table “adminlogin” contains three fields:

id – primary key – auto increment
username – varchar(100)
password – varchar(100)

The datatype for name and password is varchar. The size can be altered as per the requirement. However, 100 is sufficient, and the datatype for “id” is int and a primary key. A primary key also called a primary keyword, is a key in a relational database unique for each record. It is a unique identifier, such as a driver’s license number, telephone number (including area code), or vehicle identification number (VIN).

Your table structure should look like this:

table structure

Or copy and paste the following code into the SQL panel of your PHPMyAdmin.

DROP TABLE IF EXISTS `adminlogin`;
CREATE TABLE IF NOT EXISTS `adminlogin` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `username` varchar(100) NOT NULL,
 `password` varchar(100) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

To do this from SQL Panel refer to the following screenshot:

create a table from SQL panel

4. Insert values in the table: Here, we insert two records in our table. You can add as many as you want.

inserting records

Or copy and paste the following code to insert records into the SQL panel.

INSERT INTO `adminlogin` (`id`, `username`, `password`) VALUES (NULL, 'admin', 'admin'), (NULL, 'admin2', 'admin2');

After insertion, the table will look like this:

table records

PHP Code: Create a “connection.php” file. This is the connection code in PHP, to connect the database with any of your PHP DOM pages.

PHP




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


Include the “connection.php” file inside the PHP DOM page.

PHP




<?php
    include_once('connection.php');
?>


Congratulations, you have successfully connected your database with the PHP DOM page.

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads