PHP & MySQL: Creating Connection to MySQL Database
By rancidTaste
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:
- servername: This is an optional parameter. It specifies the server for creating connection. The default value of servername is localhost:3306.
- username: This one is an optional parameter too. It means the user name to log in to the database.
- 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
Recent Pages of itis123
- How to install Twitter widget into your blog?
Twitter, is becoming very popular day-by-day and a free social messaging utility for staying connected in real-time with your friends.
- How to add "Top of the page" icon to your blog?
"Top of the page" icon link is an important thing if your blog post is long.
- How to find out and change the size of your blog's header image?
Blogger header image gives the uniqueness of your blog among the millions of blogs.
- Troubleshooting
If you are a compute user, then you may face several types of problems at different times.
- ESET NOD32
Virus, Malware, Trojan, Worm, Spyware, Adware, Botnet etc.
- NOD32 keys
ESET NOD32 is one of the best effective and most proactive antivirus software and malware protection.
- How to Get Free ESET NOD32 Antivirus Key And Password
Different antivirus software are used to protect our PC form virus.
- Changing Your Blog's Header Image
You can easily add your favorite image or photo as a blog's header image.
rancidTaste's Recent Pages
- Drawing a line: How to draw a line using HTML 5?
HTML5 canvas API provides basics tools and great supports to draw and style different types of sub paths including lines, arcs, Quadratic curves, and Bezier curves, as well as a means for creating paths by connecting sub paths. - 5 days ago
- SEO Pages
SEO, Search Engine Optimization and Keywords - these common three terms are the brainstorming terms for any content publisher, seo company, seo service provider, seo researcher, seo firm, seo consultant, seo professional or other seo lover. - 3 months ago
- WordPress.org vs. WordPress.com: An explained comparison between WordPress.org an WordPress.com
WordPress is a common name but do you know that there are two things: WordPress. - 3 months ago
- Creating database from cpanel: How to create a database from cpanel hosting?
A step by step full tutorial to create a database from cpanel hosting account for any dynamic website. - 3 months ago
- Natural Join in Database: Retrieving records from more than one table with Natural Joins
Natural Join is one of the sql joining. - 3 months ago
- Explanation of SQL Join: Obtaining data from multiple tables using Join
Explanation of SQL Join: Obtaining data from multiple tables using Join. - 3 months ago
- How to solve "CreateFile error 32 when trying set file time" Error During Oracle Installation
Solution of the Oracle installation problem on Windows 7 / Windows XP during Oracle installation start-up. - 3 months ago
- A Brief Explanation of SQL Statements and Classification
Any type of database is based on several statements. - 3 months ago







BindraBindu 7 months 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 !!!!