You are here: simplifying repeating database connections in a class? « PHP Programming Language « IT news, forums, messages
simplifying repeating database connections in a class?

Posted by news on 11/24/77 11:27

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();

 

Navigation:

[Reply to this 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

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