note 17464 deleted from functions by jimw
| From: | jimw@php.net | Date: | Thu, 20 Dec 2001 23:18:29 +0000 |
| Subject: | note 17464 deleted from functions by jimw | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-23551@lists.php.net to get a copy of this message | ||
There is no possibility in the PHP to use many connections at the same time. But you may create a
class for database connections to handle the querys more easily.
Example:
<?
if(!defined("_INCLUDE_DATABASE_"))
{
define("_INCLUDE_DATABASE_",TRUE);
//***************************************************************
// Manages database operations
// (c) 10.11.2001 by Alexander Filatow (a.filatow@numlock.de)
//***************************************************************
class CDatabase
{
//***************************************************************
// Constructor
//***************************************************************
function CDatabase($filename)
{
$this->SetFile($filename);
$this->Load();
$this->Connect();
}
//***************************************************************
// Sets file for settings
//***************************************************************
function SetFile($filename)
{
$this->filename = $filename;
}
//***************************************************************
// reads the file infos
//***************************************************************
function AnalyzeContent()
{
// split content into lines
$lines = explode("\n", $this->contents);
for ($i=0;$i<sizeof($lines);$i++)
{
// split each line in 2 halfs
$half = explode(":", $lines[$i]);
// analyze the content
if (eregi("server", $half[0]))
$this->server = ltrim(rtrim($half[1]));
if (eregi("user", $half[0]))
$this->user = ltrim(rtrim($half[1]));
if (eregi("password", $half[0]))
$this->password = ltrim(rtrim($half[1]));
if (eregi("database", $half[0]))
$this->database = ltrim(rtrim($half[1]));
}
}
//***************************************************************
// loads the settings
//***************************************************************
function Load()
{
// get contents of a file into a string
$fd = fopen ($this->filename, "r");
$this->contents = fread ($fd, filesize ($this->filename));
fclose ($fd);
$this->AnalyzeContent();
}
//***************************************************************
// sets the connection
//***************************************************************
function Connect()
{
$this->db = mysql_pconnect($this->server, $this->user, $this->passwort);
mysql_select_db($this->database,$this->db);
}
//***************************************************************
// executes a query.
// second argument switches a database, if set
//***************************************************************
function Query($query,$dbase = NULL)
{
if ($dbase)
{
if (!$result = mysql_db_query($dbase,$query, $this->db))
$this->ErrorHandler("<b>Error in the Query:</b> ".$query);
}
else
{
if (!$result = mysql_query($query, $this->db))
$this->ErrorHandler("<b>Error in the Query:</b> ".$query);
}
return $result;
}
//---------------------------------------------------------------
// Variables
//---------------------------------------------------------------
var $db; //database pointer
// default settings
var $server;
var $user;
var $password;
var $database;
} // end of class
} // end of define
else die ("ERROR INCLUSION");
?>
The configuration file (database.cfg in my case) must have following format:
Server: localhost
User: [your username]
Password: [your password]
Database: carmen
To use the class on your page, just use following:
include ("class.database.php");
$db = new CDatabase("database.cfg");
// usage example:
$result = $db->Query($query);
...