How to insert data in MySql Database using PHP

In this tutorial I am going to explain how we can connect PHP application with database. As we know that to connect with database first we need to enter the server name, user id and password.

Same process we have to follow with the PHP connectivity too. We have some system defined method to work with database.

Connect with MySql

First we have to connect with database and check whether database connected successfully or not. To connect with database we use mysql_connect function. Syntax of this function is as follows:-

Syntax of mysql_connect function


 mysql_connect(‘database’,’username’,’password’)
		

We have to pass three parameters to this function. In first parameter we have to pass the database name. If you are working on local system then you will pass localhost as database name and if you are working on live database then you can pass ip address of server or server name.

In second parameter you have to pass username. If you are working on local system then you can pass root as username. Then we have to pass the password.

Note:-if you are working on local system then you can skip the password.

When this function gets executed and connected successfully then it will return MySQL link identifier.

For Example


<?php
$con = mysql_connect('localhost', 'root', '')
 or die("Database not connected successfully");

?>

		

Now execute the code and you will get following output:-

Database connectivity using PHP Figure 1

Select Database

In php after connecting with MySql now you have to select the database in which you want to start working. If database selected successfully then we have to run queries in php.

mysql_select_db function

We use mysql_select_db function to select the database

We pass database name and connection identifier as parameter. And if database selected successfully then we can use it.

For Example


<?php
$con = mysql_connect('localhost', 'root', '')
 or die("Unable to connect to MySQL");
	
		$isdb=mysql_select_db('isha_db',$con)
			or die("Database not connected");
?>

		

You will get following output:-

Database Selection using PHP Figure 2

Insert Data in PHP

After connecting the connection and database now you are able to execute the queries.

Suppose I want to insert some data in database. I have emp table in which I want to insert some data.

In this example I am not taking input from my html page. In my next article I will discuss how we can insert data in PHP by taking input from the html controls:-


&<?php
$con = mysql_connect('localhost', 'root', '')
 or die("Unable to connect to MySQL");
	
		$isdb=mysql_select_db('isha_db',$con)
			or die("Could not select isha_db");
				$id=11;
$name="Neha";
$age=23;
				$query="insert into emp values('$id','$name','$age')";
				$result=mysql_query($query,$con);
					if($result!="")
					{
					echo "Inserted successfully";
					mysql_close($con);
					}
					else
					{
					echo "Not inserted";
					}
			
?>

		

Now execute this code and then check the database

output of insertion Figure 3

As you see that records has been inserted successfully.