|
Main (PHP)
3rd Party Streams
Resources
Code Snippets
Affiliates
|
|
|
| |
Connecting to MySql |
| By admin (2009-02-27. 8242 views.) |
Describe how to connect to a MySql instance |
|
The standard function for connecting to a MySql instance or server is the function call mysql_connect(). The function takes several arguments:
<?php
$handle = mysql_connect($server, [$username, $password, $newlink, $clientflag]);
?>
Where:
$handle is the returned connection made. If this value is empty an error occurred which can be retrieved using mysql_err()
$server is the address of the MySql instance. If you are running on a desktop this would typically be "localhost". In a shared/production environment this will typically be the ip address or dns name of the database server.
$username is the username to use as far as access control to the database. You may have several different 'user's within the database, each with their own set of access rights to the database. This field is optional and defaults to the database owner.
$password is the password associated with the username provided. This field is optional and defaults to "". You will hardly every see a default password used.
$newlink is an optional flag that tells PHP to attempt to use an existing connection with the same credentials, if it is available. Again, you will hardly ever see this in use in a production environment.
$clienflag contains optional settings concerning the connection. Not normally used, but possible:
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
By default the connection will automatically be closed when PHP finishes processing the script for the page in use.
You can place an '@' sign in front of the call to avoid errors being thrown into your page generation. This is a commonly used technique. |
| |
|
| |
|
|
|
|
|
|
|
|
Top Sponsor
Sponsors
Sponsors
Advertisting
Affiliates
|
|