English
German

MySQL Connect

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

Example:

mysql_connect(servername,username,password);

 

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

// some code
?>

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 ""

Closing a Connection

The connection will be closed automatically when the script ends. To close the connection before, use the mysql_close() function:

Example:

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

// some code

mysql_close($con);
?>


PHP Tutorial,MySQL Connect, MySQL Connect example, learn MySQL Connect,explain example MySQL Connect online free training PHP Tutorial, PHP Tutorial example, learn MySQL Connect, online tutorial, download tutorial, PHP Tutorial books, PHP Tutorial videos, live videos PHP Tutorial, learn PHP Tutorial, PHP Tutorial topic MySQL Connect, live training PHP Tutorial, download free tutorial