Go Back   Wiki NewForum > Career Forum (Documents/Tutorials/Advice) > Tech Forum & Tutorial > PHP Forum & Tutorial

PHP Forum & Tutorial PHP Tutorial, Codes, Examples & Full End-To-End Process

Reply
Views: 946 Source: CNN18.com, India-Forums, TellyChakkar, India, Times of India, Youtube, BollywoodLife, Rediff  
Thread Tools Rate Thread Display Modes
  #1  
Old 05-04-2009, 05:44 PM
welcomewiki welcomewiki is offline
Member
 
Join Date:
Dec 2008
Location:
India
Posts: 39,034
Default PHP MySQL Connect to a Database

The free MySQL database is very often used with PHP.
Create a Connection to a MySQL Database

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.


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:




$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:

$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
} // some code mysql_close($con);
?>

Similar Threads:
Ads
Reply With Quote
Reply

New topics in PHP Forum & Tutorial

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Forum Jump


Powered by vBulletin® Version 3.8.10
Copyright ©2000 - 2018, vBulletin Solutions, Inc.
asp web stats