All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

MySQL Connection

You can connect to a MySQL database easily with PHP using mysql_connect() function it returns TRUE on success or FALSE on failure.



Code Explanation

  • host: it is an optional parameter it takes the value of hostname. If not specified, then default value is localhost:3036.

  • username: it is an optional parameter the username that access the database. Generally the default value is "root".

  • password: it is an optional parameter the password of the username that access the database.The default value is empty.

Example of to Connect To MySQL Database using PHP


   $host = 'localhost:3036';
   $user = 'root';
   $pass = '';

   $conn = mysql_connect($host, $user, $pass);

   if(! $conn )
     die('Could not connect: ' . mysql_error());
   echo 'Connected successfully';


To Disconnect From MySQL Database

You can disconnect from MySQL database using PHP mysql_close() function.

❮ PrevNext ❯