Database connection to fetch data in PHP

PHP is a very famous Server side scripting language. The famous Facebook has been built on PHP (Cake PHP) and it as numerous faces in the web world. On the face of the Internet, many websites, applications, and services which were being used by a large number of other applications, and Users take PHP as one of their best tool to work out things.As a beginner, one should be able to contact the databases using the database connection.

Name it Oracle, MySQL, MongoDB, etc., you need to connect it with your application to use the data stored in the server. Here given the techniques to connect to various DataBases with our applications using PHP.

MySQL Database Connection using PHP

PHP has an inbuilt function that connects to MySQL database using mysql_connect() function. It takes 5 parameters and returns MySQL link identifies on a successful connection and a FALSE on failure.

$connection = mysql_connect(server, mysql_db_user, mysql_db_password, new_link, client_flag)

The server parameter is an optional value while it takes the default value as localhost:3306
mysql_db_user is an optional string that the MySQL server takes as the user to login. The default value is user.
mysql_db_password is an optional string that the mysql_db_user is entailed with a password. The default password is an empty value.
new_link is an optional parameter to hold the connection identifier if a new connection is tried to make without closing the previously opened connection.

client_flag is an optional value which is a combination of the following values.
MYSQL_CLIENT_SSL − For SSL encryption
MYSQL_CLIENT_COMPRESS − For compression protocol
MYSQL_CLIENT_IGNORE_SPACE − For allowing space after function names
MYSQL_CLIENT_INTERACTIVE − For allowing interactive timeout seconds of inactivity before closing the connection

After the date is fetched or added to the database, we need to close the connection for better using the database security. The following command is used to close the database connection.
bool mysql_close ( resource $link_identifier );

$dbhost = ‘localhost:3306’;
$dbuser = ‘user_name’;
$dbpass = ‘password123’;
$connect = mysql_connect($dbhost, $dbuser, $dbpass);

if(! $conn ) {
die(‘Could not connect: ‘ . mysql_error());

echo ‘Connected successfully’;

MongoDB Database Connection using PHP

In order to use MongoDB with PHP, it requires mongo Drivers to understand the commands.

Also, add the following line to php.ini file so that the mongo drivers get the fullest use and compile properly.
extension = php_mongo.dll

Using the following commands, you can connect the MongoDB server with your PHP application.
When creating a new connection to MongoDB, we need to specify the database name in the connecting statement. 

$m = new MongoClient(); This line creates a connection to the database.
$db = $m->mydb; This line selects a database to work with, after a successful connection.

echo “Database mydb selected”;

After the successful connection and selection of database, we can work on database operations.

Oracle Connection using PHP

Before connecting to Oracle DB directly from PHP, we need to configure the server using the following settings.
In the php.ini file,

  1. Set doc_root = c:apacheapache2htdocs
  2. Set the extension_dir to: extension_dir = “C:phpext”
  3. Uncomment (Remove the semicolon “;” from the beginning of the line) this line: session.save_path = “c:/temp”
  4. Uncomment the line: extension=php_oci8.dll


  5. Open the httpd file using any text editor, which exist on the path C:ApacheApache2conf
  6. Find the line AllowOverride None and change the None value to be All
  7. Look for the line DirectoryIndex index.html index.html.var and replace it with this line: DirectoryIndex index.html index.html.var index.php
  8. Search for a section that has a lot of AddType commands, then added this line: AddType application/x-httpd-php .php so u have a total of 3 AddType command.
  9. Search for a section has many Loads and add this line to it:
    LoadModule php5_module modules/php5apache2.dll

Restart the apache server and Oracle Database.
Using the following code you can have a successful connection to Oracle DB from PHP.

Now after checking the connection, operations on database can be performed.
tnsnames.ora file gives the HOST details and SID value is equal to your database name.
Then save the file with the name test.php

Open a web browser page and type on the title: http://localhost/test.php

If it all worked well you will get a white page with the result:
Successfully connected to Oracle.

Read my other posts: PHP Vs Angular