PHP - MySql Connection Example

Learn to connect to MySql using PHP. We will insert a record in MySql datatbase using PHP.
Submitted by Jyoti Singh, on January 31, 2018 [Last updated : March 13, 2023]

Create MySQL schema and a table

First, you need to create a schema named "Product". In this schema create a table named "product" with following fields.

product schema for PHP example

Now you have your database ready! It's time to make an HTML form to get the values from the user. Make a file in your PHP folder named "product.php" and write the following code in it as it is just html you would be able to understand the code easily.


	<form action="productsubmit.php">
			<b><font color="Green" size="5">Product Interface</font></b>
		<td><b><i>Product Id</i></b></td>
		<td><input type="text" name="pid"></td>

		<td><b><i>Product Name</i></b></td>
		<td><input type="text" name="pn"></td>

		<td><b><i>Product Rate</i></b></td>
		<td><input type="text" name="pr"></td>

		<td><b><i>Offer Rate</i></b></td>
		<td><input type="text" name="por"></td>

		<td><input type="file" name="pic"></td>

		<td><b><i>Purchase Date:</i></b></td>
		<td><input type="date" name="pd"></td>
		<td><input type="submit"></td>
		<td><input type="reset"></td>

Here your form is ready let's try how it looks. Run your product file in your browser and you should see like this:

PHP - MySql Connection HTML form

Now let's code to insert this data into the database. To do this make a file named "productsubmit.php" and add the following code in it.

PHP Code


	$cn=new PDO($dsn,"root","123");
	//In order to connect to the database we use PDO, 
	//PDO stands for php data object which is used to 
	//accessthe database,u need to give three parameters in it 
	//first one is database and host name second 
	//is The user id of database and password.
	//request to get the data 
	$nq="insert into products values($pid,'$pn',$pr,$por,'$pic','$pd')";

		echo "Record Submitted";
		echo "Fail to submit record";

Code/Variable explanations

  • $dsn → this is a variable in which we define our host for now it's localhost and dbname holds the database name.
  • PDO → In order to connect to the database we will use PDO, PDO stands for php data object which is use to access the database , it takes three parameter name of the database , userid of database and password.
  • $nq → this variable holds the insert query that is to be process.

This query will be prepared and executed by using inbuilt functions prepare() and execute(). $result will contain the result sent by the database. If record will be insert successfully $result will be true else it would be false and we will show the message accordingly.

Let's submit the record and see the result:

PHP - MySql Connection output

Good work! Product is submitted to table product you can check it in database. Explore this make changes in this code and le it behave as you want it to be.

PHP Database Programs »

All PHP Programs

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.