You are currently viewing PHP Tutorial: Working with Databases
PHP: Working With Databases

PHP Tutorial: Working with Databases

Hello my all dear Visitors, welcome again in this domain ( we have been learned various PHP topics in our previous tutorial, You can also jump to the previous topic using the right-click on the PHP category in the right sidebar. So now, we are going ahead and look How PHP works with databases and How do we connect our PHP script within the database?

Which Database server can be used with PHP Script?

Connecting PHP to databases on the server is natural, and MySQL is the best database server to connect with your PHP script for storing, manipulating, and displaying your stored data. you can also use mySQL server on your localhost through XAMPP server. XAMPP stands for Cross-platform, Apache, MariaDB(Mysql), PHP, and Perl. This is very easy to install and open your MySQL server when you want to do it. One of the most popular ways to work with PHP is to handle databases on the server.

Instead of MySQL, a lot of databases exist to attach with your PHP script, because PHP is a highly supported language with a lot of databases. you can find the manuals for them at, By far, the most frequently used database system with PHP is MySQL.

First of all, we will need to understand some basic points, such as-:

What are Databases?

Databases are nothing, but it contains heterogeneous data in an organized way. Most of the databases contain data in row and column format. every row in the database is called a record of the database, each and every column in the database is called a field. MySQL is not a database, it is a database management system software, With the help of this software, you can add, manipulate, and delete a lot of databases that you want, because MySQL is the database server for its users to use them appropriately.

What is the need for a MySQL database with PHP?

As we all know before, MySQL is the database management system software to manage various kinds, sizes, and credential databases. Instead of MySQL, other database system software also exists to manipulate databases and their tables. But, MySQL is the best one to communicate with PHP scripts, it really needs to save or store user data in one place separating with multiple column names and their values.

For Example-: You have filled a form for online exam registration and click the submit button to send your form to the Server, after sending your form to the Server, the Server fetch your data and manipulates your data to check that is it right or wrong filled, if it is right, then Server script (PHP script) send your data to the database for storing the data on the Server machine. After storing your data in the database, you can log in to the server for the next time, when you visit on the Server with the help of your stored data in the Server database machine.

What is the SQL?

SQL means (structured Query language), a structured Query language is a type of computer language that is used to give the instruction for fetching the data from the database server. This is also called high-level language because all of the instructions are in alpha-numeric format. Users use those pre-defined literals, Constants, and variables to make the appropriate query.

To interact with databases in PHP, you use SQL(Structured Query Language).

Query means – A combination of instruction that is used to fetch the data from the database or to the database. In other words, Query is a combination of pre-defined words that are made to manipulate your data with the database system.

Some essential SQL Queries – We are going to take a look at the some SQL Query here-

  • create table table-name (column-name variable-type, column-name variable-type, ——– ) [Note -: Black font text are user-defined and red font text are pre-defined] – [This SQL query for creating a new table in the selected database.]
  • insert into table-name values (‘prajjwal’,’8707′); [This SQL Query is for inserting the new record at a pre-defined table in the selected database.]
  • select * from table-name [This SQL Query is for accessing the data from the database.]
  • delete from table-name where column-name=”column-value”. [This SQL Query is for deleting the record from the pre-defined table in the selected database.]

Write a PHP Script to connect and manipulate with the MySQL database

First, ensure that Your both Server (Apache and MySQL) is running or not on your localhost machine. if both turned on and running on your localhost machine, you can write your script for connecting the PHP Script with the MySQL database.

Apache and MySQL server are running on the user machine

PHP provides many built-in functions to connect with MySQL database.

You can use those function to connect, read, and write your data in the MySQL database.

Those pre-defined functions writes with your PHP script and then execute it.

Ok, this is enough knowledge for now, we will write a PHP script with pre-defined database connection functions.

<!DOCTYPE html>
Sign Up Form
<meta name="viewport" content="width=device-width, initial-scale=1.0">
if(isset($fname) && isset($lname) && isset($email)
 && isset($pass) && isset($pass_c) && isset($mobile))
"password") or die("Server is not connecting.....");
$select_db=mysqli_select_db($connection,'DB_Name') or die ("not connected to india_go DB");
$query="insert into signuppage values
$result=mysqli_query($connection,$query) or
 die("Query Failed:");
  echo "<h1>Form is successfully submited......</h1>";
<h1 id="main_head">Sign up Form (User Registration)</h1>
<div class="container">
<div class="contain">
<form name="SignUp_Form" method="post">
First Name<input type="text" name="User_FN" required></br>
Last Name<input type="text" name="User_LN" required></br>
Email ID<input type="email" name="Email_ID" required></br>
Password<input type="password" name="U_Pass" required></br>
Re-type Password<input type="password" name="U_R_Pass" required></br>
Mobile Number<input type="tel" pattern="^\d{10}$" required name="U_Mobile"></br>
<input type="submit" id="sub_butt" name="button" value="Sign UP"></br>

Describe the above things –

First of all, we have to write PHP script with the help of html tags to make the sign up form, then connect your PHP script with MySQL database server and insert your typed data into the selected database.

  • mysqli_connect(); – This function connects your PHP script with database. three arguments are mandatory – mysqli_connect(server_name, username, user-password);
  • mysqli_select_db(); – This function selects your intended database. two arguments are mandatory- mysqli_select_db(connect_object,db_name);

Last Words-: This is enough for now, we will meet very soon with next PHP article. I hope you will enjoy my articles. For more technical updates, you can follow us on the social networks. Ok Dear Visitors, Thank you so much for reading this tutorial till the end, stay tuned with us as always.

Share post

Prajjwal Singh

Tech Blogger || Web developer || Computer Networking Enthusiast || Microsoft SQL Database Management Expert || Software Debugger || Learned DOS OS Structure

Leave a Reply