• My PHP Pages

    This is a complete and free PHP programming course for beginners. Everything you need to get started is set out in section one below.

Jun 28, 2013

Posted by Unknown
No comments | 8:29 AM


Lets connect mysql database using php. there is some special php functions for this..

mysql_connect(host,username,password)

Host - host is optional its host name of mysql database. most of the time its localhost

username - mysql username

password - mysql password


There is a another function to do this.

mysqli_connect(host,username,password,dbname)

Note : it is mysqli_connect()  this is new method php introduced "i" stands for improvements . there is number of improvements in this functions. you can find them in php documentation 


Close mysql connection

mysql_close() or mysqli_close()


Select Database

mysql_select_db(database_name,connection_string);

Database name - Default database to connect with.

Connection string - Connection string is output of  mysql_connect()

Ok now lets make simple connection to mysql database using this two methods...



//make connection
$con = mysql_connect("localhost","root","123");
//check connection
if(!$con)
{
die('Could not connect to the database:'.mysql_error());
}
//select database
$db=mysql_select_db("my_db",$con);
 if(!$db)
{
die('Could not connect to the database:'.mysql_error());

}

//close connection
mysql_close($con);





0 comments :

Post a Comment