Before you can access data in a database, you must create a connection to
the database.
In PHP, this is done with the mysql_connect() function.
Syntax
mysql_connect(servername,username,password);
Parameter | Description |
---|---|
servername | Optional. Specifies the server to connect to. Default value is "localhost:3306" |
username | Optional. Specifies the username to log in with. Default value is the name of the user that owns the server process |
password | Optional. Specifies the password to log in with. Default is "" |
Note: There are more available parameters, but the ones listed above are the most important. Visit
our full PHP MySQL Reference for more details.
Example
In the following example we store the connection in a variable ($con) for later use in the
script. The "die" part will be executed if the connection fails:
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// some code
?>
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// some code
?>
Closing a Connection
The connection will be closed automatically when the script ends. To close the
connection before, use the mysql_close() function:
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// some code
mysql_close($con);
?>
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// some code
mysql_close($con);
?>
PHP is the basic common web developing tutorials also genetic tool for creating the dynamic web pages and websites.
ReplyDeleteBest Website Design Bangalore
I hope this program detail would be helpful for beginners in the field of MYSQL development.Every MYSQL developers should keep the daily update in website development and make a great ideas to build a unique style the website.
ReplyDeleteWebsite Development Bangalore
A website design has to be user friendly.The website has to be interactive and fresh in order to leave a mark on the minds of users.
ReplyDeleteWebsite Designing Companies India | Website Development Company
Choosing the right web development company is the important as the website is vital for their business.
ReplyDeleteOutsource Web Designers | Outsource Site Development
Choosing a website is very important to grow your business.Website should be user friendly to all.
ReplyDeleteWeb Design Companies in Coimbatore | kovai e solutions
Best SEO Services Affordable Packages and Prices - SEO Experts in coimbatore, chennai Tamil Nadu, India Search engine Optimization Company in india
ReplyDeleteWell i like this one your work is very good and I appreciate you and hopping for some more informative posts.
ReplyDeleteVillas in Kovaipudur
Thank you for sharing this post about the best techniques for PHP Development, This is very useful for PHP Development and PHP Development Company.Website Designing Bangalore | Website Development Bangalore
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteBakery wordpress themes is an exclusive choice for various food businesses, bakeries and restaurant chains. You require no investment for this theme.
ReplyDelete