How to connect the Database with PHP DOM page ?
Last Updated :
31 May, 2022
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.
Similar Reads
How to fetch data from the database in PHP ?
Database operations in PHP are a very crucial thing that is especially needed in CRUD (Create, Read, Update and Delete) operations. In this article, we will discuss the Read part i.e. data fetching from database. There are two ways to connect to a database using PHP. They are as follows. MySQLi ("i"
4 min read
How to retrieve data from MySQL database using PHP ?
There are steps to understand for retrieving the data from the MySQL database. Approach: Create the database, then create the table for data.Enter the rows in the table. You have to connect to the database. Now we understand each and every step as shown below.  Example 1: In this. we use PHPMyAdmin
2 min read
How to make a connection with MySQL server using PHP ?
MySQL is a widely used database management system that may be used to power a wide range of projects. One of its main selling features is its capacity to manage large amounts of data without breaking a sweat. There are two approaches that can be used to connect MySQL and PHP code, which are mentione
3 min read
How to add a PHP page to WordPress?
Adding a custom PHP page to your WordPress site can be useful for various reasons, such as adding unique functionality or creating custom templates. This article will walk you through the process step by step.1. Create a WordPress Template PageStep 1: Login to Microsoft Web Matrix:Open or create you
2 min read
How to Create a New Database in phpMyAdmin?
We have seen many apps uses any specific backend database or services which are used to store data of the app within itself. In this article we will take a look on creating a new SQL database in phpMyAdmin so that we can perform CRUD operations using that database. In this article we will be simply
3 min read
How to get the source code of a web page using PHP ?
Given a webpage, for which we need to find its source code using PHP. For this, we are going to use the PHP htmlspecialchars() function which converts any predefined characters to their subsequent HTML entities.Example 1: Suppose we take a sample website that looks like the below image, let us see w
2 min read
How to secure database passwords in PHP?
Most of the websites are providing sing up and login facility to the user. User has to create a password and use it for login to the website. But it is very important to secure the password of the user. password_hash() function provides the facility to securely store the password of the user to the
1 min read
How to Connect ReactJS as a Front-end with PHP as a Back-end ?
Connecting the front end of React with the PHP backend is a classic approach to building a web application with the front end for the user interface and the back end or server side for handling databases, programming logic, and other operations.In this article, weâll discuss the steps required to co
3 min read
How to embed PHP code in an HTML page ?
PHP is the abbreviation of Hypertext Preprocessor and earlier it was abbreviated as Personal Home Page. We can use PHP in HTML code by simply adding a PHP tag without doing any extra work. Example 1: First open the file in any editor and then write HTML code according to requirement. If we have to a
2 min read
How to Update Data in MySQL Database Table Using PHP?
Updating data in a MySQL database table using PHP is a fundamental aspect of web development, particularly in applications where user interactions involve modifying existing records. This guide delves into the process of updating data in a MySQL database table using PHP, covering database connection
3 min read