RCRAMER.COM
Clever website slogan
Home > Tech > PHP > MySQL

PHP MySQL - Connecting to a 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. The connection will automatically close when the script ends but, you may close it earlier with the mysql_close() fucntion.

Example:

<?php $con = mysql_connect("localhost","username","password"); if (!$con) { die('Could not connect: ' . mysql_error()); }
// some code

mysql_close($con);
?>

mysql_connect()

The mysql_connect() function is used to open a non-persistent MySQL connection. This function returns the connection on success, or FALSE and an error on failure. You can hide the error output by adding an '@' in front of the function name.

mysql_connect(servername,username,password,newlink,clientflag);
ParameterDescription
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 ""
newlink Optional. If a second call is made to mysql_connect() with the same arguments, no new connection will be established; instead, the identifier of the already opened connection will be returned
clientflag Optional. Can be a combination of the following constants:
  • MYSQL_CLIENT_SSL - Use SSL encryption
  • MYSQL_CLIENT_COMPRESS - Use compression protocol
  • MYSQL_CLIENT_IGNORE_SPACE - Allow space after function names
  • MYSQL_CLIENT_INTERACTIVE - Allow interactive timeout seconds of inactivity before closing the connection

To establish a persistent MySQL connection, use mysql_pconnect() instead.

mysql_close()

The connection to the database will be closed as soon as the script ends. To manually close the connection before, use mysql_close(). This function returns TRUE on success or FALSE on failure.

mysql_close(connection);
ParameterDescription
connection Optional. Specifies the MySQL connection to close. If not specified, the last connection opened by mysql_connect() is used.

Connecting to an ODBC

ODBC is an Application Programming Interface (API) that allows you to connect to a data source (e.g. an MS Access database).

The odbc_connect() function is used to connect to an ODBC data source. The function takes four parameters: the data source name, username, password, and an optional cursor type.

The odbc_exec() function is used to execute an SQL statement.

$conn=odbc_connect('northwind','',''); $sql="SELECT * FROM customers"; $rs=odbc_exec($conn,$sql); odbc_close($conn);

Here is how to create an ODBC connection to a MS Access Database:

  1. Open the Administrative Tools icon in your Control Panel.
  2. Double-click on the Data Sources (ODBC) icon inside.
  3. Choose the System DSN tab.
  4. Click on Add in the System DSN tab.
  5. Select the Microsoft Access Driver. Click Finish.
  6. In the next screen, click Select to locate the database.
  7. Give the database a Data Source Name (DSN).
  8. Click OK.

Note that this configuration has to be done on the computer where your web site is located. If you are running Internet Information Server (IIS) on your own computer,