how to connect php with mysql in xampp

Here is the code and steps in the video you can refer the solution for your problem on how to connect php with mysql using Xampp Server. We use phpMyAdmin as a database administrative tool to sync with mysql and php which bridge connection between web client and server.

What is Xampp?

Xampp is an abbreviation for cross-platform, Apache, MySQL, PHP, and Perl, and it allows you to build a WordPress site offline, on a local web server on your computer. Xampp is an open-source software developed by Apache Friends. The use of Xampp is to test the clients or your website before uploading it to the remote web server.

To know about how to download and install Xampp server on local pc computer you can read this article Read More.

To Connect php with MySQL database you need to install XAMPP which is an easy to install Apache distribution containing MariaDB, PHP, and Perl.

I had demonstrated this problem with a simple HTML registration form written in PHP having some 4 to 5 fields in it and connect with mySql phpMyAdmin database administrative tool using Xampp Server i.e localhost server.

Below is the Video & Code for step guide on how to connect html registration form and php mySql using Xampp Server and its setting to establish the phpMyAdmin database with the localhost server.

HTML Registration Form(sample_register.html)

how to connect php with mysql in xamppDetails Entry Form demo


<html>
<head>
	<title>
		A Sample Tutorial for database connection.
	</title>
</head>
<body bgcolor="#32e692">
	<div align="center">
		<!--<h1>Details Entry Form</h1>-->
	</div>
<form action="details_entry.php" method="post">
	<table border="1" align="center">
		<tr>
			<td>
			<label>Enter First Name</label></td>
			<td><input type="text" name="first_name"></td>
		</tr>
		<tr>
			<td>
			<label>Enter Last Name</label></td>
			<td><input type="text" name="last_name"></td>
		</tr>
		<tr>
			<td>
			<label>Gender</label></td>
			<td><input type="radio" name="gender" value="male">Male
			<input type="radio" name="gender" value="female">Female</td>
		</tr>
		<tr>
			<td>
			<label>Enter Email</label></td>
			<td><input type="email" name="email"></td>
		</tr>
		<tr>
			<td>
			<label>Enter Phone</label></td>
			<td><input type="phone" name="phone"></td>
		</tr>
		<tr>
			<td colspan="2" align="center" ><input type="submit" name="save" value="Submit" style="font-size:20px"></td>
		</tr>
	</table>
</form>
</body>
</html>

Details Entry Form

details entry form how to connect mysql with php using xampp
details entry form how to connect mysql with php using xampp

details_entry.php

//These are the main settings to establish your connection with mysql database it could vary as per your system

<?php

$server_name=”localhost”;

$username=”root”;

$password=””;

$database_name=”database123″;

$conn=mysqli_connect($server_name,$username,$password,$database_name);
//now check the connection
if(!$conn)
{die("Connection Failed:" . mysqli_connect_error());}
if(isset($_POST['save']))
{
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$gender = $_POST['gender'];
$email = $_POST['email'];
$phone = $_POST['phone'];
//For inserting the values to mysql database 
$sql_query = "INSERT INTO entry_details (first_name,last_name,gender,email,mobile)
VALUES ('$first_name','$last_name','$gender','$email','$phone')";
if (mysqli_query($conn, $sql_query))
{
echo "New Details Entry inserted successfully !";
}
//Full Code of php file for mySql database connection with html form
<?php
$server_name="localhost";
$username="root";
$password="";
$database_name="database123";

$conn=mysqli_connect($server_name,$username,$password,$database_name);
//now check the connection
if(!$conn)
{
	die("Connection Failed:" . mysqli_connect_error());

}

if(isset($_POST['save']))
{	
	 $first_name = $_POST['first_name'];
	 $last_name = $_POST['last_name'];
	 $gender = $_POST['gender'];
	 $email = $_POST['email'];
	 $phone = $_POST['phone'];

	 $sql_query = "INSERT INTO entry_details (first_name,last_name,gender,email,mobile)
	 VALUES ('$first_name','$last_name','$gender','$email','$phone')";

	 if (mysqli_query($conn, $sql_query)) 
	 {
		echo "New Details Entry inserted successfully !";
	 } 
	 else
     {
		echo "Error: " . $sql . "" . mysqli_error($conn);
	 }
	 mysqli_close($conn);
}
?>

Here are the steps of setting the connection and running the HTML registration form with phpMyAdmin MySQL database using xampp server(localhost) to execute select query.

Check this video

xampp mysql connection steps (2) (1)
xampp mysql connection steps

Also Read How to download and install the Xampp server on windows 64bit

To Download the latest Xampp Server click download xampp 7.4.6 / PHP 7.4.6/8.0.0

You can also read to this article for BSc computer science syllabus 2021 Read the article

you can also read to this article for bsc IT syllabus 2021 Read article

Important Questions for JAva Practical Questions Read More

Read this article for Important Programming Languages a B.Sc computer science/Bsc IT student must know Read article


7 thoughts on “how to connect php with mysql in xampp”

  1. Pingback: PHP basics for beginners | Techniyojan

  2. Pingback: Php Connexion Mysql - ConnexionGuider

Leave a Comment

Your email address will not be published. Required fields are marked *