Reply to Re: simplifying repeating database connections in a class?

Your name:

Reply:


Posted by news on 10/08/06 11:27

Jerry Stuckle wrote:
> news@celticbear.com 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

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация