STEP 1: CREATE A CONNECTION

  • First step is we have to create a Connection to hold our database connection.
  • In this step we give details like server information the username and password for the mysql client
  • The PHP Mysql function to connect is 'my_sql_connect()
  • We pass in 3 Parameters the servername,username and password in order.
$connection=mysql_connect("locahost","root","password");
if(!$connection)
{
     die();
}
  • Note : The die function is optional but it is a good programming practice

STEP 2: SELECT THE DATABASE

  • After succesful connection we have to use the database we need to use
  • So the Next step is to select the required database
  • The PHP Mysql function is mysql_select_db()
  • We pass in 2 parameters the database name and the connection string
$db_select=mysql_select_db("dbname",$connection);if(!$connection)
{
     die();
}

STEP 3: PERFORM THE QUERY

  • Next is to perform the query on the selected database.
  • So we use the function mysql_query()  to execute a query
  • The output resultset is held as the return variable
$result=mysql_query("Select * from table name",$connection)

STEP 4: USE RETURNED DATA

  • The output in result variable is an result set,we can't access it directly
  • So we can access the elements inside using mysql_fetch_array() function
  • We can loop through the elements using any loop in php
  • Here we are not checking inside loop but assigning for every iteration
  • we can access the required field by using the variable name followed by the field name
while($row=mysql_fetch_array($result))
{
echo $row['fieldname'];
}

STEP 5: CLOSE THE CONNECTION

  • It is a good practice to close the open database connection
  • The function for closing connection is mysql_close()
mysql_close($connection);

If u couldn't follow any of the above explanations feel free to comment below !  


Thank u for reading this  Article hope it was useful !

-Regards

Sathesh B.M

About Sathesh Bm

Sathesh Bm has written 26 post in this website.

Engineer, Web Lover, Thinker, Blogger

Related Posts Plugin for WordPress, Blogger...