Fahid Javid

Fahid Javid


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.

November 2018
M T W T F S S
« Jan    
 1234
567891011
12131415161718
19202122232425
2627282930  

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 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 connection

It is very important when you want to start a dynamic website to create connection with your SQL (we are talking about mysql) by using mysql_connect() function. In mysql_connect() arguments should be 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 database and connection we made earlier.

3.  Perform database query

In this step we have to select out data from 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 fetch 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 it 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 4
  • 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 🙂