Nodejs and NPM have made life so easier. If you use Gulp toolkit with it I am sure you love it 😍 However, I came...
PHP Database Interaction in FIVE stepsFahid 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:
- Create connection
- Select database
- Perform database query
- Use return data
- 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.