Re: More frustration with MySQL and PHP

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux