MySQL CONNECT

From JumbaWiki

Jump to: navigation, search

This is a general code to use when connecting or disconnecting to and a MySQL database. This code isn't considered definitive and can be written in many varied ways.

It is generally accepted that you try to limit database connections for better efficiency when loading your pages. You should also disconnect from your dataase when you've finished with the calls to it.

PHP

//Define variables (generally best to include from a config file, to make it easier to update later):
$host = "localhost";
$username = "username";
$password = "password";
$database = "databasename";
 
//Connect code:
mysql_connect("$host", "$username", "$password") or die("Unable to connect to database");
mysql_select_db("$database") or die("Could not select database $database");
 
//ANY DATABASE CALLS CAN GO HERE
 
//Disconnect code:
mysql_close();

ASP

<%
Dim DB_Connection, objConn , objRS
 
DB_Connection = "DRIVER={MySQL ODBC 3.51 Driver}; SERVER=localhost; DATABASE=databasename; UID=username; PASSWORD=password; OPTION=3"
 
Set objConn = Server.CreateObject("ADODB.Connection")
 
objConn.Open(DB_Connection)
 
//Database Calls go here
 
objConn.Close
Set objConn = Nothing
%>

See also

MySQL Basics
Connect/Disconnect | Select | Insert | Update | Delete
Personal tools
Australian Photography | Unique Views of Reality | Daedalus