PHP & MySQL: Creating Connection to MySQL Database

MySQL database is widely used with PHP. Before using MySQL with PHP, you must have to create a connection to the database of MySQL. If you don't create the connection then you can't access to database form the php code. This page describes the process of creating connect to MySQL database. This is done by using mysql_connect() function. To learn totally, just keep reading...

How to create connect to MySQL

Actually, MySQL is a database. You can access MySQL database from php by using one function. The function is mysql_connect( ). If you don't use this function, you can't connect the database from php. mysql_connect( ) has three parameters. The full function looks like the following line:

mysql_connect (servername, username, password) ;

mysql_connect() opens a non-persistent MySQL connection. From the above line, you can find that mysql_connect() has the following three parameters:

  1. servername: This is an optional parameter. It specifies the server for creating connection. The default value of servername is localhost:3306.
  2. username: This one is an optional parameter too. It means the user name to log in to the database.
  3. password: This is an option parameter also. It's the password of the user to login the database. If there is no password then the value will be " ".

Codes for creating connection

To connect the database you have to write some code using mysql_connect() function. One example is provided below:

/* creating a connection using mysql_connect() function */

<?php

$myConnection = mysql_connect("localhost","user","abc123");

if (!$myConnection)
  {
    die('Not possible to connect...' . mysql_error());
  }

/* all other codes */

?>

Explanation of the above code

From the above code, you can see that we declare mysql_connection() function by a variable $myConnection. mysql_connection() has three parameters. (1) localhost means the servername, (2) user means the name of the user to login the database and (3) the corresponding user's password, in this case the password is abc123.

Later die() function is used. It's used to verify i.e. if you don't able to connect to the database then it will show the message Not possible to connect...

That's all to create a connect to database of MySQL form php code. In the next writing, I will explain the connection closing process and it's importance.

© Written by rancidTaste

If you are enjoyed this post, please consider to give a thumbs up and leave a comment. You may Subscribe to rancidTaste's RSS feed to get new pages which will be delivered to your feed reader. You can also read more hubs by rancidTaste.

Want to express your knowledge and ideas, click here to join Hubpages.

It's simple, just like 1-2-3

More by this Author


Comments 1 comment

BindraBindu 5 years ago

Hi,

nice one article, it helps a lot !!!!! thanks for sharing with us.

Here is also a nice one article on how to connect php webpage with mysql database step by step, you may chek out the following url for more details......

http://www.mindstick.com/Articles/1ac687d9-f163-46...

Thanks !!!!

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working