PHP database connectivity!!
As everyone knows PHP is web scripting language in simple terms its the language used for creating webpage or website. And its really important to know how to use PHP to connect to databases as all the website which are in existence right now use database as back-end to store information in a secure way as well as organized way. So in this tutorial I am gonna show you how to establish connection to a database using PHP. Lets get it started....
First lets see how to establish a connection to a database and then we will see how to execute simple query in order to show the fetched result set to users through webpage.
In order to establish a connection to MYSQL there is a predefined function called mysql_connect which takes three parameters, the first parameter is the host name ( the machine IP address where the mysql is residing ) and the second parameter is the username (mysql username) and the third parameter is the password (mysql password for that user name you have specified as second parameter) each parameter is separated from one another using a , (comma). So the connection string will be as shown below:
mysql_connect("host", "username", "password");
NOTE: If the database your trying to access is available in the same machine you can specify localhost as host name (That's the first parameter)
Ok now lets pass some real parameters to this connection string,
mysql_connect("localhost", "slashfear", "arvind");
Ok your done!! the above connection string will connect to local mysql server with the user name slashfear and password arvind, that's how you can connect to database using PHP simple right!!
For other databases like oracle, postgresql, sybase, db2 the predefined function as well the parameters will change and are listed below for your reference:
Oracle database connection:
oci_connect("username", "password", "host");
Postgresql database connection:
pg_connect("host=hostname port=5432 dbname=demo user=slashfear password=arvind");
Sybase database connection:
sybase_connect('host', 'username', 'passoword');
DB2 database connection:
db2_connect("DATABASE=demo;HOSTNAME=host;", "username", "password");
Now we know how to connect to database, its important to know how to disconnect from it too so in-order to disconnect from a mysql database we have to call the predefined function mysql_close which will take one parameter which is the reference to the connection which your willing to close so lets look at a an example for clear understanding:
$dbcon = mysql_connect("localhost", "slashfear", "arvind");
mysql_close($dbcon);
So in the above snippet the variable $dbcon reference to the connection to mysql which connects to the local mysql server with the user name slashfear and identified by password arvind. So in order to disconnect from the database we call the function called mysql_close and pass the variable name which holds the reference to the connection string.
Following is the list of predefined functions to perform disconnection from other database:
Oracle database disconnection:
$dbcon = oci_connect("username", "password", "host");
oci_close($dbcon);
Postgresql database disconnection:
$dbcon = pg_connect("host=hostname port=5432 dbname=demo user=slashfear password=arvind");
pg_close($dbconn);
Sybase database disconnection:
$dbcon = sybase_connect('host', 'username', 'passoword');
sybase_close($dbconn);
DB2 database disconnection:
$dbcon = db2_connect("DATABASE=demo;HOSTNAME=host;", "username", "password");
db2_close($dbconn);
Continuation of this tutorial is on PAGE 2 😀
If you have any doubts please feel free to ask!!
-Arvind