PHPsu
MANUAL ZH  |  EN
     


Current Position :| index>Beginners> PHP HOW Connect to a MySQL Database

PHP HOW Connect to a MySQL Database

FROM: AUTHOR: TIME:2008-04-20 HITS:

The free MySQL Database is very often used with PHP. www.phpsu.com


Connecting to a MySQL Database

Before you can access and work with data in a database, you must create a connection to the database. www.phpsu.com

In PHP, this is done with the mysql_connect() function. welcome to phpsu.com

Syntax

mysql_connect(servername,username,password); 
http://www.phpsu.com

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.

welcome to phpsu.com

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:

do you kown phpsu.com?

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
} phpsu.com
// some code phpsu.com is a free phpscool 
?> 
phpsu is a phpschool


Closing a Connection

The connection will be closed as soon as the script ends. To close the connection before, use the mysql_close() function. phpsu is a phpschool

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
phpsu.com
// some code phpsu提供的php教程 
mysql_close($con);
?>
phpsu提供的php教程

TITLE:PHP HOW Connect to a MySQL Database
Copyright 2008 The PHPsu All rights reserved. This mirror generously provided by: .Hp Inc.
Last updated: Fri Jun 6 22:56:34 GMT-8 2008