To connect the database with PHP DOM page, do the following steps:
1. Create Database: Create a database using XAMPP, the name of database is “loginpage”. You can give any name to your database.

2. Create Table: Create table, inside “loginpage” database.

3. Create Table structure: Table should contain fields, according to your needs.
For example:
- id – primary key – auto increment
- name – varchar(100)
- password – varchar(100)
- How to connect SQL Server database from JavaScript in the browser ?
- How to pass form variables from one page to other page in PHP ?
- How to display search result of another page on same page using ajax in JSP?
- How to show Page Loading div until the page has finished loading?
- How to redirect a page to another page in HTML ?
- Create a database on Relational Database Service (RDS) of Amazon Web Services(AWS)
- Nodejs - Connect MongoDB with Node app using MongooseJS
- Nodejs – Connect Mysql with Node app
- How to connect multiple MySQL databases on a single webpage ?
- How to connect to an API in JavaScript ?
- How to Display Flash Messages using connect-flash Module in Node.js ?
- How to connect Django with Reactjs ?
- Node.js socket.connect() Method
- PHP Program to count Page Views
- Refresh a page using PHP
- How to add API function to a simple PHP Page ?
- How to create admin login page using PHP ?
- How to minify HTML code of PHP page ?
- How to add a PHP page to WordPress?
- PHP program to fetch data from localhost server database using XAMPP
The datatype for name and password is varchar. The size can be alter as per the requirement. However, 100 is sufficient, and the datatype for “id” is int and it is a primary key. A primary key, also called a primary keyword, is a key in a relational database that is unique for each record. It is a unique identifier, such as a driver license number, telephone number (including area code), or vehicle identification number (VIN).

The structure of table will look like this

4. Insert values in the table:

After insertion, the table will look like this:

PHP Code: Create “connection.php” file. This is the connection code in PHP, to connect the database with any of your PHP DOM page.
<?php $conn = ""; try { $servername = "localhost:3306"; $dbname = "loginpage"; $username = "root"; $password = ""; $conn = new PDO( "mysql:host=$servername; dbname= loginpage", $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 include_once('connection.php'); ?> |
After this, the database is connected with the PHP DOM page.
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

