我发现了类似的主题,但是许多结果使用mysqli\u multi\u查询,这是我希望避免的,因为将来会实现用户生成的查询。
目前,我正在使用下面的php实现我想要的结果,但是我有一种感觉,我缺少一种更有效的方法。我还担心并发查询和这样一个复杂的进程占用连接的可能性。尽管任何时候的userload都不应该大于20。
这里是代码,任何批评或见解欣赏(我仍然在学习php,因此我的代码在这方面可能是狗屎!):
表模式(这需要很好地工作—从本质上说,我需要开发最佳结构):
CREATE TABLE `EAM`(
`EAM_ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`EAM_IPADDR` VARCHAR(15) NOT NULL,
`EAM_PORT` INT(5) NOT NULL,
`EAM_STATE` VARCHAR(6) NOT NULL);
CREATE TABLE `ACTIVE`(
`EAM_ID` INT NOT NULL,
`ACTIVE_STATUS` VARCHAR(25) NOT NULL,
`ACTIVE_TIME` TIME DEFAULT NULL,
FOREIGN KEY (EAM_ID) REFERENCES EAM(EAM_ID));
CREATE TABLE `MAP_IMG` (
`MAP_IMG_BLDG` varchar(25) NOT NULL,
`MAP_IMG_ROOM` varchar(25) NOT NULL,
`MAP_IMG_X` int(8) DEFAULT NULL,
`MAP_IMG_Y` int(8) DEFAULT NULL,
`MAP_IMG_ROOM_STATUS` varchar(10) NOT NULL DEFAULT 'NOTCLEAR');
CREATE TABLE `LOCATION`(
`LOCATION_ROOM` VARCHAR(25) NOT NULL PRIMARY KEY,
`LOCATION_BLDG` VARCHAR(25) NOT NULL,
`EAM_ID` INT NOT NULL,
`LOCATION_COMMENT` VARCHAR(250) DEFAULT NULL,
FOREIGN KEY (EAM_ID) REFERENCES EAM(EAM_ID));
$dbQuery = "SELECT EAM_ID FROM LOCATION WHERE LOCATION_BLDG = 'LQ1'"; //TODO: Eventually make this 'LQ1' a variable for page selected.
$dbQueryResult = mysqli_query($dbConnection, $dbQuery) OR DIE("Bad Query: $dbQuery");
echo "<table class='table table-striped'><thead>";
echo "<tr><th>BUILDING</th>";
echo "<th>ROOM NUMBER</th>";
echo "<th>ROOM STATUS</th>";
echo "<th>LAST UPDATE</th>";
echo "<th>EAM IP ADDRESS</th>";
echo "<th>EAM PORT</th></tr></thead>";
while($row = mysqli_fetch_assoc($dbQueryResult)) {
$eamID = $row['EAM_ID']; //Assign EAM_ID from initial query to $eamID for use throughout subsequent queries.
$dbq2 = "SELECT LOCATION_BLDG, LOCATION_ROOM FROM LOCATION WHERE EAM_ID = $eamID";
$dbqr2 = mysqli_query($dbConnection, $dbq2) OR DIE("Bad Query: $dbq2");
while($r2 = mysqli_fetch_assoc($dbqr2)) {
echo "<tr><td>{$r2['LOCATION_BLDG']}</td>";
echo "<td>{$r2['LOCATION_ROOM']}</td>";
}
$dbq2 = "SELECT ACTIVE_STATUS, ACTIVE_TIME FROM ACTIVE WHERE EAM_ID = $eamID";
$dbqr2 = mysqli_query($dbConnection, $dbq2) OR DIE("Bad Query: $dbq2");
while($r2 = mysqli_fetch_assoc($dbqr2)) {
echo "<td>{$r2['ACTIVE_STATUS']}</td>";
echo "<td>{$r2['ACTIVE_TIME']}</td>";
}
$dbq2 = "SELECT EAM_IPADDR, EAM_PORT FROM EAM WHERE EAM_ID = $eamID";
$dbqr2 = mysqli_query($dbConnection, $dbq2) OR DIE("Bad Query: $dbq2");
while($r2 = mysqli_fetch_assoc($dbqr2)) {
echo "<td>{$r2['EAM_IPADDR']}</td>";
echo "<td>{$r2['EAM_PORT']}</td></tr>";
}
}
1条答案
按热度按时间9bfwbjaz1#
不要在循环中执行查询来获取相关数据,而是在顶部使用一个查询来返回数据。