procedural php

We knew all the fundamentals of php. Now, we can work on Procedural PHP that's the basic php script. There are a few things we need to do while working on php.

Make connection to the database.

Execute database queries.

Process results.

Free up memory.

Close connection. (OPTIONAL)

Let's create some files to work on the insert, select, delete and upadate operations on database. For those operations we need to make connection to the database. It's always better to create a separate file for database connection and include it in all files rather than writing connection query in each file. Let's create a separate file named connect.php for database connection that we'll include in all operation files. But before that, we need to create a database. Go to localhost/phpmyadmin and create a database first. Here' i'll create one with the name 'proceduralphp'.


We have got a database but no tables so, lets add a table in the database. You know how to create a table now. You can open to the database and select the option create new table and fill up the forms or go to the SQL option and add query to create table. Here, i'm going with the second option.

CREATE TABLE user_info(
username varchar(255) DEFAULT NULL,
email varchar(255) UNIQUE,
password varchar(255),
gender ENUM('male','female'),
language SET('nep','eng','oth'),
country varchar(255),

ENUM lets us enter only one of the defined values while SET allows us to enter multiple values from the defined ones. If any value except the defined ones is attempted to be entered, the defined column will be empty for that record.

Click on go and the table will be created.


Php has an inbuilt function to make connection to the database that asks for a few arguments. This function will return some resources that we need to hold in some variables.


All the arguments ask for string values in the function. The first one is the hostname. The second one is the database username that will be 'root' for local database by default. We can go to the database and from the priviledge option we can create specific username and password for each database too. The third one is database password that will be empty for local database by default and the last one is the database name. We can add all the values in the query directly but the best and organized option would be to create constants for those values and add them in the query.


$connect = @mysqli_connect(HOST,USER,PASSWORD,DB);

	die ('<h1>Error in establishing Database Connection.</h1>');

This connect.php file will make connection to the database we defined and in case of error will print the defined message. We can suppress the php warning here with @ operator and display only that message we defined.