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

Your name:

Reply:


Posted by Jerry Stuckle on 09/29/47 11:27

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
>
> class e_test {
>
> function gettech() {
> include('../Connections/userDBconnect.php');
> $db = mysql_select_db("printing", $connection) or $error .= "Could
> not select database1: " . mysql_error()."<p />";
> $dbconn = $connection;
> // CREATE TECH ARRAY
> $sql_T = "SELECT email FROM tbl_users WHERE dept = '1' AND
> active='1'";
> $result_T = @mysql_query($sql_T, $dbconn) or $error .= "Could not
> query1: " . mysql_error()."<p />";
> $count_T = mysql_num_rows($result_T) or $error .= "Could not select
> query2: " . mysql_error()."<p />";
> $i_T = 0;
> while ($row_T = mysql_fetch_array($result_T)) {
> $email_T = $row_T[email];
> $techs .= $email_T;
> $i_T++;
> if ($i_T < $count_T) {
> $techs .= ",";
> }
> }
> echo $error;
> return $techs;
> }
>
> function getdez() {
> include('../Connections/userDBconnect.php');
> $db = mysql_select_db("printing", $connection) or $error .= "Could
> not select database2: " . mysql_error()."<p />";
> $dbconn = $connection;
> // CREATE DESIGNER ARRAY
> $sql_D = "SELECT email FROM tbl_users WHERE dept = '3' AND
> active='1'";
> $result_D = @mysql_query($sql_D, $dbconn) or $error .= "Could not
> query3: " . mysql_error()."<p />";
> $count_D = mysql_num_rows($result_D) or $error .= "Could not query4:
> " . mysql_error()."<p />";
> $i_D = 0;
> while ($row_D = mysql_fetch_array($result_D)) {
> $email_D = $row_D[email];
> $designers .= $email_D;
> $i_D++;
> if ($i_D < $count_D) {
> $designers .= ",";
> }
> }
> echo $error;
> return $designers;
> }
>
> function getall() {
> $this->group = $techs.",".$designers;
> $thelist = $this->gettech().",".$this->getdez();
> return $thelist;
> }
>
> }
>
> $maillist = new e_test();
> echo $maillist->gettech();
> echo "<p />";
> echo $maillist->getdez();
> echo "<p />";
> echo $maillist->getall();
>

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.

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.

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.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[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

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