Re: More frustration with MySQL and PHP

From: Date: Tue, 22 Jan 2008 14:37:06 +0000
Subject: Re: More frustration with MySQL and PHP
References: 1 2 3 4 5 6 7 8  Groups: php.general 
Request: Send a blank email to php-general+get-267832@lists.php.net to get a copy of this message
Jason Pruim wrote:
So all I have to do (At least to really simplify what I'm sure they can do) is put the code I want to execute into the function and then just list the variables as arguments? Such as: function dbconnect ($host, $user, $pass, $database) { mysqli_connect($host, $user, $pass, $database); etc. etc. } Or something like that? And then I can simply call my dbconnect() anywhere I might need to connect to a database? Or a different database?
Sort of. I believe the MySQLi extension is intelligent enough to not create multiple connections with the same details from one process, but you might find the following works a bit better...
    http://dev.stut.net/php/db.inc.phps
This is modified from code I used to use but I have not tested this simplified version (the original supports multiple connections which you probably don't need). I now use a class instead of these functions but the basic idea is the same. At any point in your code you can simply to do...
    $db = GetDB();
...to get a resource handle to your database, and it takes care of making sure it only keeps one connection around and closes it when the script ends. Yes it uses globals. No I don't care. -Stut -- http://stut.net/

« previous php.general (#267832) next »