Skip to content
Related Articles

Related Articles

How to connect the Database with PHP DOM page ?
  • Difficulty Level : Expert
  • Last Updated : 01 Jun, 2020

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)
  • 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.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?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();
    }
       
    ?>

    chevron_right

    
    

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

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    After this, the database is connected with the PHP DOM page.




    My Personal Notes arrow_drop_up
Recommended Articles
Page :