Here is a "quick and dirty" way to get a connection to a MySQL server and a database selected. There are newer and better ways of doing this. If you use a shared hosting provider, however, they are not always so quick to update the servers to the latest software.

This method should work with PHP 4+ and MySQL 4+ and provides two benefits: The connection function can be used by all your scripts for all your databases. The connection for each database - user is kept in its own file. If you are only ever going to make one page, then this will look redundant. If you plan to have several pages that access databases, however, this could be useful.

WARNING: Do not keep ANY usernames and passwords in files on your server if you don't completely understand what is world-readable. Understand the Apache 1.3 or 2.0 htaccess files(1.3) or htaccess files(2.0).

// Sample data file for dbconn() function. $strHostName = "HOST_NAME"; //e.g., "localhost". $strDbName = "DB_NAME"; //Check if yours have prefixes. $strUserName = "USER_NAME"; //For the database, not your hosting account. $strPassword = "PASSWORD";

These boxes represent two text files kept in the path of your PHP include_path in the php.ini file. The first is food for the second and meant to be duplicated for different host- user- password combinations.

function dbconn($strHostName, $strDbName, $strUserName, $strPassword) {    //    // MYSQL CONNECTION FUNCTION    //    // 2006-08, 2008-09    //    // Parameters: Host Name, Database Name, Username and Password for MySQL.    //    // Provides: MySQL Connection Resource and Database Selection.    //    // Make the connection global. Return it if it exists.    // Pilfered from the PHP online manual page:    //    global $rsrcDbLink;    if($rsrcDbLink) {       return $rsrcDbLink;    }    // Use the mysql_connect funtion to connect to database    // or generate a readable error message.    $rsrcDbLink = mysql_connect($strHostName, $strUserName, $strPassword);    if (!$rsrcDbLink) {       echo "<br />MySQL SERVER CONNECTION ERROR.<br />\n";       return false;    }    //Use the mysql_select_db function to select a database    // or generate a readable error message.    if (!mysql_select_db($strDbName, $rsrcDbLink)) {       echo "<br />DATABASE SELECTION ERROR: ".mysql_error()."<br />\n";       return false;    } }


  • The files containing the variables and the function must be placed / included before the function is called.
  • If you do not specify a database for the rest of your script, MySQL will assume the latest value provided by this funtion.
  • An update form's "action" I was using, in the form of another PHP page, would not work until the global snippet was put in the function. I read a lot of comments about how bad globals are, so use with caution. I only program for my own use.

Here are examples of how it looks when used in a script. Include the files that contain the function code and data. Mine are in a directory called "db" within the include file location (the php.ini include_path). Then just excecute the function. The variables will be available from the included data file.

<?php    //Database Connection    require "db/";    require "db/";    dbconn($strHostName, $strDbName, $strUserName, $strPassword);    ...Work with database here... ?>

Here is just the data example file and function in plain text:
dbdat file
dbconn function

Addendum:If you try to use a mysql_close function and get the error "Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource", then this type of connection might be useful. The script had probably closed the connection before you issued the command.