Fahid Javid

Fahid Javid


April 2024
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930  

Categories


PHP Database Interaction in FIVE steps

Fahid JavidFahid Javid

PHP has a pretty straight forward method to working with MySQL databases. I will explain to establish a connection to the MySQL database, but  I am not going to go into SQL which is the language used to put data in and get data out of a database. There are five steps to make PHP database interaction and I will explain each one in detail with example code:

  1. Create a connection
  2. Select database
  3. Perform database query
  4. Use return data
  5. Close connection

Here is the PHP Database Interaction FIVE steps Code Example:

1. Create a connection

It is very important when you want to start a dynamic website to create a connection with your SQL (we are talking about MySQL) by using mysql_connect() function. In mysql_connect() arguments should be a string and it’s important to use die() function with mysql_error() function to check if there is a problem with the connection or not.

2. Select database

Now we have to select the database which we are creating and saving our data by using mysql_select_db() function which the arguments are the name of the database and connection we made earlier.

3.  Perform database query

In this step, we have to select out data from the database and bring it into our web page. The best decision to use mysql_query() function which it will Send a MySQL query with 2 arguments as displayed in the above code.

4. Use return data

To display the result on your web page, you have to use while() loop function in addition to mysql_fetch_array() function which fetches a result row as an associative array, a numeric array, or both.

5. Close connection

To close connection, it is better to use mysql_close() function to close MySQL connection. This is a very important function as it closes the connection to the database server. Your script will still run if you do not include this function. And too many open MySQL connections can cause problems for your account. This is a good practice to close the MySQL connection once all the queries are executed.

My name is Fahid and I’m a Full-Stack WordPress Developer. I started working with WordPress community and I’m finding myself learning something new every single day.

Comments 6
  • Harshad
    Posted on

    Harshad Harshad

    Reply Author

    Great Work..👍🏻


  • Bittu
    Posted on

    Bittu Bittu

    Reply Author

    Very helpful for my exams in php… Its an very imp question for tha…tysm😇


  • owais khan
    Posted on

    owais khan owais khan

    Reply Author

    Thanks for sharing this wonderful knowledge 😊


  • Niyaz Ahmad
    Posted on

    Niyaz Ahmad Niyaz Ahmad

    Reply Author

    Thanks you for explaining me PHP Database connectivity in this Easiest way.


  • pokemon online
    Posted on

    pokemon online pokemon online

    Reply Author

    Excellent article. I wіll be experiencing а few of these issues as ѡell..


    • fahidjavid
      Posted on

      fahidjavid fahidjavid

      Reply Author

      Thanks Pokemon! Wish you good luck 🙂