All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Connect HTML To Database With MySQL

Last Updated : Mar 11, 2024

How To Connect HTML To Database With MySQL

In this tutorial we will show you the solution of how to connect HTML to database with MySQL, before connection there must a software named as XAMPP must be installed to your computer or laptop.

Or it you want to use any other platform you can use this accordingly. Now, after installation the next process and code to connect with database is described below.

Step By Step Guide On How To Connect HTML To Database With MySQL :-

Now, the next process is to create a database under phpmyadmin and also to create a table under the database. Here, below we show you some codes and later we will discuss these in deep.

Here, we did not create any html page, we will only show you code in php to show you how to connect html with database.

The codes are as:

<?php
    $connect=mysqli_connect("localhost","root","","as1") or die(" Connection Failed");
    $query="insert into students(name, marks) values('John', 65)";
    if(mysqli_query($connect,$query))
    {
        echo" WE INSERT YOUR RECORD SUCCESSFULLY";
    }
    else
    {
        echo" WE ARE SORRY, BUT YOUR RECORD IS NOT INSERTED";
    }
?>
  1. First, here we already said that we did not use any html code here. But we here show you how you are able to connect html with database and this can be done with the help of php.
  2. Now, let us understand the codes. First here we create opening and closing tags of php that are <?php ?>. Where <?php is opening tag of php and ?> is closing tag. All the contents of php are always written inside these tags.
  3. After this inside php tags, we create a variable named connect and make a connection with database. $connect=mysqli_connect("localhost","root","","as1") or die(" Connection Failed");
  4. Here, we use inline query of php than is mysqli_connect to make a connection with database. Under this first we use server name, then user name, then password used and at last database name.
  5. If due to some issue we are not able to make a connection then die will execute and connection failed displayed on browser screen.
  6. Whereas in next step we use $query, this is used here to insert data into database or table to show you that connection is established between html and database. Inside this, students is the table name that we create under database as1, which is already discussed.
  7. And after this name, marks are columns of table students, whereas john, 65 are values which we want to insert in table.
  8. At last we create if and use mysqli_query under this with values $connect, $query. This is, if we want to enter some data in database through html.
  9. Here, if everything is right then “WE INSERT YOUR RECORD SUCCESSFULLY” this line goes printed. If not then else part will be executed. We hope that you understand the article properly.

Conclusion :-

In conclusion, here we can say that after this article you are able to make a connection between html's to database with the help of MySQL.

But as we know that we did not use html here, whereas connection goes established and in next session we will create a form in html and show you how to save data in database.

I hope this tutorial on how to connect HTML to database with MySQL helps you.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪