Saturday, February 25, 2012

Php Database

Welcome to PHP Database Basics.

PHP (Hypertext Preprocessor) like every other server side script works with all sorts of databases. Here we will discuss database connectivity under various other related issues.

Lets say that you already know basic PHP syntax and you already have a working environment of PHP/MySQL  installed on your PC. It does not really matter what OS you are on as the syntax and the configuration will be somewhat similar.

Win32 environment being very common and widely used can be taken as the first scenario.

Application used to deploy PHP, MySQL, Apache is WAMP. You may check WAMP related details by clicking here http://www.wampserver.com/en/  .

Hoping that WAMP installations successful and all the other details like PHP/MySql are running we will proceed to basic declarations and syntax.

Chapter 1.

Basic Database Connection.

The first thing to do is to connect to the database.

eg. $con = mysql_connect("localhost","username","password");


Breakup

$con = Variable that stores the connection settings.
mysql_connect = Argument that ask the server to connect to the database.
localhost = Address of the server. (This is localhost as we are using wamp. We will see other examples too.)
username = The username to connect to the server.
password = Password to connect to the server.

In the above scenario we see that the you are connecting to the database server using a address to the database, username and password to access the database. The command breakup has all the details mentioned above.

* Remember that when you create a database in WAMP/ PHPMyAdmin you need to also create a user and  grant it all the rights. Post granting all the rights you will be able to use the above statement. This is very important.


Checking If The Database Can be Connected or Not.


As a good practice you should check if your PHP is able to talk to the database server or not. I mean is it connecting using the above statement or not. The below commands will let you do that.


$con = mysql_connect("localhost","username","password");
if (!$con)
  {
  die('The PHP did not connect: ' . mysql_error());
}
else
                                        {
                                          ..............rest of the stuff if connection happens.

Breakup

$con = Database connection declaration.
if(!con) = A if condition stating if not $con then what to do
die() =  Killing the active connection.
'The PHP did not connect: ' = This is a custom message that you can write for you reference. This will show  then the connection fails.
mysql_error() = This will actually display what went wrong. It will tell you if your address is wrong or your username/password is wrong.
else = If the connection is successful then what all has to follow.

When you check the connection and if it throws all the above errors you will need to recheck your connections.


Here is a small example that you can directly copy paste, change the username password and see if your database is connecting.

<?php

$con = mysql_connect("localhost","username","password");
if (!$con)
   {
   die('The PHP did not connect: ' . mysql_error());
}
else
                                        {
                                           echo "Database has been successfully connected";

                                         }



?>

Once above all tests are successful you can proceed to further stuff like extracting data from database, select query and other important stuff and tricks.