SITE SEARCH

SITE LINKS

    • PHP Basic
  • PHP HOME
  • PHP Intro
  • PHP Install
  • PHP Syntax
  • PHP Variables
  • PHP String
  • PHP Operators
  • PHP If...Else
  • PHP Arrays
  • PHP Switch
  • PHP While Loops
  • PHP For Loops
  • PHP Functions
  • PHP Forms
  • PHP $_GET
  • PHP $_POST

  • PHP Advanced
  • PHP Date
  • PHP Include
  • PHP File
  • PHP File Upload
  • PHP Cookies
  • PHP Sessions
  • PHP E-mail
  • PHP Secure E-mail
  • PHP Error
  • PHP Exception
  • PHP Filter

  • PHP Database
  • MySQL Introduction
  • MySQL Connect
  • MySQL Create
  • MySQL Insert
  • MySQL Select
  • MySQL Where
  • MySQL Order By
  • MySQL Update
  • MySQL Delete
  • PHP ODBC

  • PHP XML
  • XML Expat Parser
  • XML DOM
  • XML SimpleXML

  • PHP and AJAX
  • AJAX Introduction
  • XMLHttpRequest
  • AJAX Suggest
  • AJAX XML
  • AJAX Database
  • AJAX responseXML
  • AJAX Live Search
  • AJAX RSS Reader
  • AJAX Poll

  • PHP Reference
  • PHP Array
  • PHP Calendar
  • PHP Date
  • PHP Directory
  • PHP Error
  • PHP Filesystem
  • PHP Filter
  • PHP FTP
  • PHP HTTP
  • PHP Libxml
  • PHP Mail
  • PHP Math
  • PHP Misc
  • PHP MySQL
  • PHP SimpleXML
  • PHP String
  • PHP XML
  • PHP Zip

 

 

Home Html Css Javascript Php Asp .Net Sql Xml
user login New user? Register Here | Forgot Password?

PHP MySQL Connect To Database

Prev
Next

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. 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 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);
?>

 

 

Prev
Ne xt
Top

 

 

© 2010 Copyrighted. Yavum.com™