Forum Moderators: coopster
My C background reminds me that variables handed to a function are copied locally for use (unless using pointers within the function of course).
I'm wondering if PHP is similar? My particular brain ache relates to database connections. EG:-
<?php
$db = &ADONewConnection('mysql');
$db->Connect($db_hostname, $db_username, $db_password, $db_database);
$sql = 'SELECT something';
$result = $db->GetRow($sql);
$bitIwant = $result['thisbit'];
?>
If I want to turn this into a function, should I hand a pointer to the function like this:-
<?php
// Keep this database in scope as we need it again later
$db = &ADONewConnection('mysql');
$db->Connect($db_hostname, $db_username, $db_password, $db_database);
$mybit = getMyBit(&$db);
function getMyBit(&$db) {
$sql = 'SELECT something';
$result = $db->GetRow($sql);
$bitIwant = $result['thisbit'];
}
?>
Or, within each function that requires a database connection should I recreate it (seems a bit wasteful)?
I'm just curious as to what is considered best practice for this.
Thanks :)
and yes you should minimize the number of connections made for efficiency.
as a matter of fact, in perl you can use the Apache::DBI module to initiate a persistent database connection [search.cpan.org].
not sure if there is a php equivalent...
Mack.
So even if you were to "create multiple connections" they aren't really happening, unless, of course, you specify the "new link" parameter as part of your mysql_connect function call, which I wouldn't normally advise.
>> I'll double-check that I don't need to reference the $db connection when using it with a function.
You shouldn't need to but I always (or try to) do it for clarity and good practice. :)
... don't confuse the perl DBI "persistent" database connection module with persistent database connections as defined in any given RDBMS with a persistent database connection feature. The HTTP protocol in and of itself is stateless so the "persistence" part of it just doesn't exist across connections/requests. I just wanted to clarify to eliminate any confusion.
You can indeed get your db connection to "persist" in a single http request transaction though and the best method for accomplishing the task in PHP (IMHO) is a singleton design pattern. It is easy to set something like this up and running quickly, the hard part is wrapping your head around it all :) Here is an example class and at the end I'll throw down a couple of helpful links. Note, the example assumes you have a separate Database class that actually performs the connection and perhaps has other properties and methods ...
/**
* Obtain the Database class as it is going to be our object instance
*/
require_once 'Database.php';
class DatabaseInstance
{
/**
* The instance of this class
* @var object
* @access private
*/
private static $_i;
/**
* Database class object
* @var object
* @access private
*/
private $_db;
/**
* Establish a connection to the database
* Private constructor prevents direct creation of object
*
* @access private
*/
private function __construct()
{
$user = 'username';
$password = 'password';
$name = 'database';
$server = 'localhost';
$this->_db = new Database($user, $password, $name, $server);
}
/**
* Creates a single instance of the database connection
*
* @return object singleton instance of the database class
* @access public
*/
public static function create()
{
self::$_i = !is_null(self::$_i) ? self::$_i : new self;
return self::$_i->_db;
}
/**
* Prevents cloning
*
* @return void
* @access public
*/
public function __clone()
{
trigger_error('Clone is not allowed.', E_USER_ERROR);
}
}
require_once 'DatabaseInstance.php';
$db = DatabaseInstance::create();
// Example of getting the link identifier
// (assuming your DB class has a public method that will return it!)
$dbLinkId = $db->getDbLinkId();
$db = &ADONewConnection('mysql');
function doSomeDbThing(){
global $db;
$sql = 'SELECT something';
$result = $db->GetRow($sql);
}