PHP Database Connection

on Sunday, November 11, 2012

MySql DB connection

Now I am going to tell You how to stabilise a connection with DB.
Syntax:             
mysql_connect(nameofserver,username,password);

Exapmle

mysql_connect(“localhost”,”root”,””) or die (“not connected”);
// localhost is use for local server
You can do it by other way also
$myhost=”localhost”;
$username=”root”;
$password=””;
mysql_connect($host,$username,$password) or die (“not connected”);

NOW , How to connect with database

Syntax:
First stabilise  the connection with server
mysql_connect(“localhost”,”root”,””) or die (“not connected”);
mysql_select_db(“myDb”) or die(“db not connected”); 

Example:

// save as conn.php
mysql_connect(“localhost”,”root”,””) or die (“not connected”);
mysql_select_db(“myDb”) or die(“db not connected”);
echo “I hv got connected”;