Re: simplifying repeating database connections in a class?




Jerry Stuckle wrote:
> news@xxxxxxxxxxxxxx wrote:
> > Well, I wrote my first PHP class today. Yeah!
> > But to get it to work, in each function within the class I have to
> > repeat the database connection lines, and that just seems redundant;
> > there has to be a better way that I'm just not bright enough to think
> > of.
> >
> > Any suggestions?
> > (It's the first 3 lines of each of the two functions below. When I have
> > it fully written, there will be about 10 similar functions, each
> > repeating those three lines.)
> >
> > Thanks for any feedback!
> > Liam
> >
> Since you're using a class (hooray!), you could open the connection in
> the constructor to your class and store it in a member variable of the
> class.
>
I tried that, evidently incorrectly, because each time the functions
within the class would run, they couldn't "see" that established
connection and the queries wouldn't work.
I tried assigning those three lines to fariables...but that so doesn't
work. Another person mentioned making it into a function...which sounds
like a great idea! I just need to figure it out.

> Another way would be to open the connection in the main part of your
> program. In this method, you could either store the connection variable
> in a member variable of your class (preferred) or pass it as a parameter
> to your functions.

I do that for all my PHP pages, have those three database est. lines at
the top. But, and I guess I'm just inexperienced enough to not know how
a SQL querey inside a function can "see" the database connection
outside it, because every time I try, I get SQL errors.
That's why I'm calling the include twice, once in each function,
because I can't seem to make it fisible to the inside of the function
otherwise.

>
> BTW - you have:
>
> include('../Connections/userDBconnect.php');
>
> twice. I suspect this is just basically a mysql_connect() call. Rather
> than include the code this way, you should place it in a function and
> include the file once (generally at the top of your script). Then you
> can call the function as necessary. They way you're doing it requires
> the server to fetch and parse the file twice - unnecessary overhead.

So, how would that work?
Something like:

function db_connect() {
include('../Connections/userDBconnect.php');
$db = mysql_select_db("printing", $connection) or $error .=
mysql_error();
$dbconn = $connection;
}
function test_sql() {
$sql_Fi = "SELECT email FROM tbl_users WHERE dept = '5' AND
active='1'";
$result_Fi = @mysql_query($sql_Fi, db_connect());
}

No, I know that won't work. OH! Like:

include('../Connections/userDBconnect.php');
$db = mysql_select_db("printing", $connection) or $error .=
mysql_error();
$dbconn = $connection;

function db_connect($sql,$dbconn) {
return @mysql_query($sql, $dbconn);
}
function test_sql() {
$sql_Fi = "SELECT email FROM tbl_users WHERE dept = '5' AND
active='1'";
$result = db_connect($sql_Fi,$dbconn);
while ($row = mysql_fetch_array($result)) {
... etc
}
}

I'll try that... but, $dbconn, to be used in test_sql() to be sent to
db_connect, needs to be sent TO test_sql()....
OK, brain melting.
If you have any good web sites that help in this manner, I'd appreciate
it. =)
Thanks for the reply!
Liam

.



Relevant Pages

  • Re: Lets Just Pretend Vista Never Happened, Shall We?
    ... System performance: requires twice the hardware of XP to operate at ... wireless connection from dropping while programs are actively using ... without requiring a reboot and/or disabling and enabling network ...
    (microsoft.public.windows.vista.general)
  • Re: Making a database connection global
    ... connection would be opened the same amount of time (which no-one yas yet ... Anyway, if he don't want to listen, a different angle will probably not ... to reference the database connection once. ... I created a class with a static database connection and the class opens ...
    (microsoft.public.dotnet.framework.aspnet)
  • Database Connection Question
    ... we were using an asp include file with database functions ... and would open and close the database connection multiple times in 1 script. ... I don't know, the persistent connection seems much better to me, easier to ...
    (microsoft.public.inetserver.asp.db)
  • Re: garbage collection
    ... it will get a reader back.. ... you can not reuse the connection on which it opened. ... > garbage collection issue, its a logical error. ... > on database connection is, ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Database Connection question
    ... the other is a standalone app and is using ... >java.sql.DriverManager for Connection. ... Every time after obtaining a ... >My task is to write generic code for database connection to be used in both ...
    (comp.lang.java.programmer)