Saturday, October 23, 2010

Database Connectivity mysql and php

Hi to all!! I am back here with a new topic in mysql php.
Today i am here to teach you what is Database connectivity is??
The word DB Connectivity may look odd but it is very simple
it just needs few basic steps to connect your front end page(eg. PHP application) with the Database. We already know what Database is??

Database connectivity:
There are many ways to connect to db with frontend application.
step 1:
we need to declare the variables.
like:
$dbhost = 'localhost'; //your localhost name
$dbuser = 'root'; // user name(set for your local server)
$dbpass = 'password'; Password(set for your local server )
where $dbhost,$dbuser,$dbpass are the variables where we store the values of our localserver.
we can make use of these variables where we need them instead of the values.
step 2:
connect to database.
like:

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

where,

$conn is a varable holding the value of input command mysql_connect.
mysql_connect is the command used to connect the database.

the above syntax means that if the $dbhost,$dbuser,$dbpass values are true and correct coonect the database.

step 3:
but with what?? so we must declare a database available in phpmyadmin
consider i have a DB called "exampledb"

So declare a variable called

$dbname = 'exampledb';

step 4:
To check or confirm that such DB exists we use the below syntax.
mysql_select_db($dbname);

The above syntax returns false if such db doesnt exist.

I conclude,

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die 'Error connecting to mysql');

$dbname = 'db';// db is the name of the database.
mysql_select_db($dbname);
?>

We need to add the required functions in between the select db and ?> to work out.

I hope you have understood what mysql PHP connectivity is.

Thanks.

Pls post comments if you have any problems with above notes.

Good luck!!!

1 comment: