简体   繁体   English

使用其他查询循环MySQL结果

[英]Looping MySQL Results With Additional Queries

I'm hoping someone may be able to help me conceptualize a solution to a PHP page I'm writing. 我希望有人能够帮助我概念化我正在编写的PHP页面的解决方案。

Effectively, I have one master table in a MySQL database called "Servers." 实际上,我在一个名为“服务器”的MySQL数据库中有一个主表。 In it are four fields; 其中有四个字段; department , serverA , serverB , serverC . 部门服务器A服务器B服务器C。 For context, the department entry is the name of a department, and serverA, serverB, and serverC correspond to hostnames of the three servers for each department. 对于上下文,部门条目是部门的名称,并且serverA,serverB和serverC对应于每个部门的三台服务器的主机名。

What I am trying to do is grab the name(s) of the department, serverA, serverB, and serverC and use that data to populate multiple SQL queries. 我正在尝试获取部门,服务器A,服务器B和服务器C的名称,然后使用该数据填充多个SQL查询。 I have my data separated into multiple tables for cleanliness and relationship. 我将数据分为多个表以保持整洁和相互关系。 Here's what I've got; 这就是我所拥有的;

<?php
$conn = mysqli_connect("server", "root", "root") or die('Error connecting to MySQLserver.');
mysqli_select_db($conn, "Database") or die("Failed to connect to database");
$sql = "SELECT * FROM Servers";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
    $deptartment = $row['department']
    $serverA = $row['serverA'];
    $serverB = $row['serverB'];
    $serverC = $row['serverC'];
}
} else {
echo "Unable to connect to server.  Please check your settings.";
}
?>

What I'd then like to do is to do a sort of foreach , wherein foreach record of serverA, the following happens; 然后,我想做的是一种foreach ,其中serverA的foreach记录发生以下情况;

<?php
$conn = mysqli_connect("server", "root", "root") or die('Error connecting to MySQLserver.');
mysqli_select_db($conn, "Database") or die("Failed to connect to database");
$sql = "SELECT * FROM Data WHERE `serverName` = '$serverA' ORDER BY `timestamp` DESC LIMIT 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
    $serverName = $row['serverName'];
    $serverType = $row['serverType'];
    $serverAge = $row['serverAge'];
}
} else {
echo "Unable to connection to server.  Please check your settings.";
}
?>

I could then use the same theory to loop through the results of serverB and have each record for serverB fulfill another query to get other data. 然后,我可以使用相同的理论遍历serverB的结果,并让serverB的每个记录执行另一个查询以获取其他数据。

I seem to be struggling with the right syntax for how to loop through each of the results and populate other queries with it. 对于如何循环遍历每个结果并用它填充其他查询,我似乎在使用正确的语法。

Any possible guidance? 有任何可能的指导吗?

Thank you! 谢谢!

Sample Data:
department: Math
serverA: math-server-windows.school.edu
serverB: math-server-mac.school.edu
serverC: math-server-linux.school.edu

department: Psychology
serverA: psychology-server-windows.school.edu
serverB: psychology-server-mac.school.edu
serverC: psychology-server-linux.school.edu

department:  Arts
serverA: artsserver.school.edu
serverB: artsold.school.edu
serverC: artsbackup.school.edu

You could use JOINs to make this much simpler. 您可以使用JOIN简化此过程。

Table Data has a field servername that corresponds to either serverA , serverB or serverC in the servers table. Data的字段servername对应于服务器表中的serverAserverBserverC So you can run a query like so: 因此,您可以运行如下查询:

SELECT Data.*,
  A.serverA AS serverA, 
  B.serverB AS serverB, 
  C.serverC AS serverC 
FROM Data
JOIN servers as A on Data.servername = A.serverA
JOIN servers as B on Data.servername = B.serverB
JOIN servers as C on Data.servername = C.serverC
ORDER BY <whatever>

So now each row will have the server information for each department, so loop through the results like so: 因此,现在每一行都将包含每个部门的服务器信息,因此可以像这样遍历结果:

while( $row = $result->fetch_assoc() ){
  echo "\nDept: ".$row['department'];
  echo "\nA: ".$row['serverA'];
  echo "\nB: ".$row['serverB'];
  echo "\nC: ".$row['serverB'];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM