Home · News · Articles · Forum · Downloads · Search Your IP: 38.103.63.58 · Today: Wednesday, November 19, 2008
Main Menu
Home
News
Articles
- General
- Common Software
- Tips & Tricks
- Networking
- Multimedia
- Operation Systems
- Hardware
- Programming
- Security
- Configs
- CMS
Forum
Downloads
Gallery
Search
FAQ
Links
Ping us
Top 10 Article Posters
empty 37
nemo 3
scary 1
tashak 1
siluni 1
linuxGuru 1
Poll
What OS are you using?

Linux

*BSD

Solaris

Mac OS

Windows

Other

You must login to vote.
PHP MySQL connection simple script

Connecting to a MySQL Database

Before you can access and work with 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
?>

Closing a Connection

The connection will be closed as soon as 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);
?>

Comments
No Comments have been Posted.
Post Comment
Please Login to Post a Comment.
Ratings
Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Search

Enter words:


Login
Username

Password



Not a member yet?
Click here to register.

Forgotten your password?
Request a new one here.
Users Online
Guests Online: 1
No Members Online
Registered: 55
Newest: Netherton216
Random Gallery Image
ntop
ntop
Screenshots
Friends
DJs Place
Fresh Flash Games
Free Wallpapers
PHP Scripts
Levski
Flash Games
Improvements
Bookmark and Share
News Feeds
Article Feeds
Forum Feeds
Spam Poison
Valid CSS!
OSGuides.net 2007 - 2008 Creative Commons License.