Re: [PHP] More frustration with MySQL and PHP



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/
.



Relevant Pages

  • Re: Multiple Connections Puzzle
    ... multiple connections to MS ACCESS database and what is the precedence of its ... I have a problem when multiple connections could not be opened at the same ... time from my code on one server but could be on another server. ... Also if I have a database open from the file system I have no problems ...
    (comp.databases.ms-access)
  • Multiple Connections Puzzle
    ... multiple connections to MS ACCESS database and what is the precedence of its ... adModeShareDenyNone in the code, ... I have a problem when multiple connections could not be opened at the same ... time from my code on one server but could be on another server. ...
    (comp.databases.ms-access)
  • "Cannot update, databse or object may be read only"
    ... I was wondering if anyone could help i have picked this database up ... On click of a button it open a recordset which is a query loops until ... Dim rstProspects As Recordset ... txtSEPARATED = "Don't Care" ...
    (comp.databases.ms-access)
  • Re: IM and Copyright Laws
    ... local area network, nor sell or offer it for sale. ... these files may not be used to construct any kind of database. ... I don't care if he did it or not. ... who act with piety towards others and their genitalia. ...
    (rec.gambling.poker)
  • Re: IM and Copyright Laws
    ... local area network, nor sell or offer it for sale. ... these files may not be used to construct any kind of database. ... I don't care if he did it or not. ... who act with piety towards others and their genitalia. ...
    (rec.gambling.poker)