mysql获取assoc和print相关的表记录

时间:2017-09-18 17:48:31

标签: php mysql

我正在尝试用PHP代码撰写电子邮件。 我想“打印”一个表中第一个表(工作组)中的一个记录,并在该记录下面记录我的第二个表(工作组成员)中的所有相关记录。在第一次记录之后,我想要在表格workgrous中进入下一条记录,并在那里做同样的事情,直到所有记录都被打印出来。 两个表中的相关字段是WorkgroupDN。 我是否正确使用db_fetch_assoc和while?

的组合

继承我的桌子:

Table ”workgroups”
CREATE TABLE `workgroups` (
`WorkgroupDN` VARCHAR(16) NOT NULL COLLATE 'utf8_unicode_ci',
`Name` VARCHAR(50) NOT NULL COLLATE 'utf8_unicode_ci',
`WorkgroupDNID` INT(11) NOT NULL,
PRIMARY KEY (`WorkgroupDNID`),
COLLATE='utf8_unicode_ci'
ENGINE=InnoDB
;

表“workgroupsmembers”:

CREATE TABLE `workgroupmembers` (
`WorkgroupDN` VARCHAR(16) NOT NULL DEFAULT '' COLLATE 'utf8_unicode_ci',
`HuntOrder` INT(11) NOT NULL DEFAULT '0',
`UserDN` VARCHAR(16) NOT NULL COLLATE 'utf8_unicode_ci',
PRIMARY KEY (`WorkgroupDN`, `HuntOrder`),
COLLATE='utf8_unicode_ci'
ENGINE=InnoDB
;

这就是我现在所得到的

global $cman;
$connection = $cman->byName("shoreware at 172.21.1.50");
$rs1 = $connection->query("SELECT WorkgroupDN, SUBSTRING(WorkgroupDN, 6, 8) AS 
extension, Description FROM workgroups INNER JOIN dn ON dn.DN = 
workgroups.WorkgroupDN WHERE BackupDN > '0' AND (SUBSTRING(WorkgroupDN, 1, 4)) 
= '".$prefix."' ORDER BY WorkgroupDN");
$data1 = db_fetch_assoc($rs1);
//Fill table with workgroups
while($data1 = mysql_fetch_assoc($rs1))
{
foreach($data1 as $field => $value){
$rs2 = $connection->query("SELECT WorkgroupDN, SUBSTRING(WorkgroupDN, 6, 8) AS 
extension, UserDN FROM workgroupmembers WHERE WorkgroupDN = 
'".$data1["WorkgroupDN"]."' ORDER BY UserDN");
$data2 = db_fetch_array($rs2);
$dnworkgroup.="
<tr><td width" . "=" . "160" . "align" . "=" . "center" . ">" . 
$data1["Description"] . "</td>" . "<td width" . "=" . "80" . "align" . "=" . 
"center" . ">" . $data1["WorkgroupDN"] . "</td>";
}
$dnworkgroupmembers.="
<tr><td width" . "=" . "160" . "align" . "=" . "center" . ">" . 
$field["UserDN"] . "</td>" . "<td width" . "=" . "80" . "align" . "=" . 
"center" . ">" . $field["UserDN"] . "</td>";
}

0 个答案:

没有答案