使用 while 循环和 if 语句从不同的表中获取数据
Fetch data from different tables using while loop and if statement
我正尝试使用 8 个不同的 table 从我的数据库中获取数据:
inventory descriptorid typeid conditionid statusid locationid stationid users
我相信我的查询工作正常问题是使用我的 $row
变量。我仅从清单 table 中的数据中获取结果。有人可以解释我如何从每个 table 中获取数据吗?
代码如下:
<?php
// get the records from the database
if ($result = $con->query("
SELECT inventory.InventoryID, descriptorid.dename, typeid.tyname,
inventory.Serial, inventory.ServiceTag, inventory.CityTag,
conditioncid.conname, statusid.statusname, locationid.locname,
stationid.stationname, users.Fname, users.Lname,
inventory.PurchaseDate, inventory.POnumber
FROM inventory
LEFT JOIN descriptorid
ON inventory.Descriptor = descriptorid.dename
LEFT JOIN typeid
ON inventory.Type = typeid.tyname
LEFT JOIN conditioncid
ON inventory.ConditionC = conditioncid.conname
LEFT JOIN statusid
ON inventory.Status = statusid.statusname
LEFT JOIN locationid
ON inventory.Location = locationid.locname
LEFT JOIN stationid
ON inventory.Station = stationid.stationname
LEFT JOIN users
ON inventory.cuserFname = users.Fname
AND inventory.cuserLname = users.Lname "))
{
// display records if there are records to display
if ($result->num_rows > 0)
{
// display records in a table
echo "<table border id='myTable' class='tablesorter' >";
// set table headers
echo "<thead><th>ID</th><th>Descriptor</th><th>Type</th><th>Serial</th><th>ServiceTag</th><th>CityTag</th><th>Condition</th><th>Status</th><th>Location</th><th>Station</th><th>CurrentUser</th><th>Lastname</th><th>PurchaseDate</th><th>POnumber</th><th></th><th></th></thead>";
echo "<tbody";
while ($row = $result->fetch_object())
{
// dump whats returned
// print_r($row);
echo "<tr>";
echo "<td>" . $row->InventoryID . "</td>";
echo "<td>" . $row->Descriptor . "</td>";
echo "<td>" . $row->Type . "</td>";
echo "<td>" . $row->Serial . "</td>";
echo "<td>" . $row->ServiceTag . "</td>";
echo "<td>" . $row->CityTag . "</td>";
echo "<td>" . $row->ConditionC . "</td>";
echo "<td>" . $row->Status . "</td>";
echo "<td>" . $row->Location . "</td>";
echo "<td>" . $row->Station . "</td>";
echo "<td>" . $row->cUserFname . "</td>";
echo "<td>" . $row->cUserLname . "</td>";
echo "<td>" . $row->PurchaseDate . "</td>";
echo "<td>" . $row->POnumber . "</td>";
echo "<td><a href='invrecords.php?InventoryID=" . $row->InventoryID . "'><img src='img/default/button_mini_ticket_edit.gif'></a></td>";
echo '<td><a href="javascript:void(0);" onclick="confirmation(' . $row->InventoryID . ');"><img src="img/default/button_mini_delete.gif"></a>';
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
您确定要使用 LEFT JOIN
而不是 INNER JOIN
吗?这意味着如果你有 table A 和 B,你只需要位于 A 但不一定在 B 中的元素。这意味着如果左边的元素没有匹配伙伴 table,您将有一行在一列中包含 table A 的元素,在另一列中包含 null
的元素。
这可能是结果集仅包含 inventory
table 的一个元素的原因之一。
据我了解,您想要一个包含以相同属性连接的元素的结果,因此请尝试将 LEFT JOIN
替换为 INNER JOIN
。
图片来源:http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins
您应该按照您 select 的方式命名您的行变量(就像您对清单 table 的列所做的那样):
错误:
echo "<td>" . $row->Descriptor . "</td>";
echo "<td>" . $row->Type . "</td>";
echo "<td>" . $row->ConditionC . "</td>";
echo "<td>" . $row->Status . "</td>";
echo "<td>" . $row->Location . "</td>";
echo "<td>" . $row->Station . "</td>";
echo "<td>" . $row->cUserFname . "</td>";
echo "<td>" . $row->cUserLname . "</td>";
改用:
echo "<td>" . $row->dename . "</td>";
echo "<td>" . $row->tyname. "</td>";
echo "<td>" . $row->conname. "</td>";
echo "<td>" . $row->statusname. "</td>";
echo "<td>" . $row->locname. "</td>";
echo "<td>" . $row->stationname. "</td>";
echo "<td>" . $row->Fname. "</td>";
echo "<td>" . $row->Lname. "</td>";
好的,所以问题不是将我的辅助 table 上的第三个选项(dename、tyname 等)与库存匹配,而是我应该将它与那个 [=] 的 ID 匹配=14=]。现在,当我使用我的行变量查询第三个选项时,我可以 select 来自我的辅助 table (deid,tyid) 的任何选项。它现在输出所需的结果,希望我可以使用类似的 query.Thanks 为所有想法创建一个下拉列表。
我正尝试使用 8 个不同的 table 从我的数据库中获取数据:
inventory descriptorid typeid conditionid statusid locationid stationid users
我相信我的查询工作正常问题是使用我的 $row
变量。我仅从清单 table 中的数据中获取结果。有人可以解释我如何从每个 table 中获取数据吗?
代码如下:
<?php
// get the records from the database
if ($result = $con->query("
SELECT inventory.InventoryID, descriptorid.dename, typeid.tyname,
inventory.Serial, inventory.ServiceTag, inventory.CityTag,
conditioncid.conname, statusid.statusname, locationid.locname,
stationid.stationname, users.Fname, users.Lname,
inventory.PurchaseDate, inventory.POnumber
FROM inventory
LEFT JOIN descriptorid
ON inventory.Descriptor = descriptorid.dename
LEFT JOIN typeid
ON inventory.Type = typeid.tyname
LEFT JOIN conditioncid
ON inventory.ConditionC = conditioncid.conname
LEFT JOIN statusid
ON inventory.Status = statusid.statusname
LEFT JOIN locationid
ON inventory.Location = locationid.locname
LEFT JOIN stationid
ON inventory.Station = stationid.stationname
LEFT JOIN users
ON inventory.cuserFname = users.Fname
AND inventory.cuserLname = users.Lname "))
{
// display records if there are records to display
if ($result->num_rows > 0)
{
// display records in a table
echo "<table border id='myTable' class='tablesorter' >";
// set table headers
echo "<thead><th>ID</th><th>Descriptor</th><th>Type</th><th>Serial</th><th>ServiceTag</th><th>CityTag</th><th>Condition</th><th>Status</th><th>Location</th><th>Station</th><th>CurrentUser</th><th>Lastname</th><th>PurchaseDate</th><th>POnumber</th><th></th><th></th></thead>";
echo "<tbody";
while ($row = $result->fetch_object())
{
// dump whats returned
// print_r($row);
echo "<tr>";
echo "<td>" . $row->InventoryID . "</td>";
echo "<td>" . $row->Descriptor . "</td>";
echo "<td>" . $row->Type . "</td>";
echo "<td>" . $row->Serial . "</td>";
echo "<td>" . $row->ServiceTag . "</td>";
echo "<td>" . $row->CityTag . "</td>";
echo "<td>" . $row->ConditionC . "</td>";
echo "<td>" . $row->Status . "</td>";
echo "<td>" . $row->Location . "</td>";
echo "<td>" . $row->Station . "</td>";
echo "<td>" . $row->cUserFname . "</td>";
echo "<td>" . $row->cUserLname . "</td>";
echo "<td>" . $row->PurchaseDate . "</td>";
echo "<td>" . $row->POnumber . "</td>";
echo "<td><a href='invrecords.php?InventoryID=" . $row->InventoryID . "'><img src='img/default/button_mini_ticket_edit.gif'></a></td>";
echo '<td><a href="javascript:void(0);" onclick="confirmation(' . $row->InventoryID . ');"><img src="img/default/button_mini_delete.gif"></a>';
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
您确定要使用 LEFT JOIN
而不是 INNER JOIN
吗?这意味着如果你有 table A 和 B,你只需要位于 A 但不一定在 B 中的元素。这意味着如果左边的元素没有匹配伙伴 table,您将有一行在一列中包含 table A 的元素,在另一列中包含 null
的元素。
这可能是结果集仅包含 inventory
table 的一个元素的原因之一。
据我了解,您想要一个包含以相同属性连接的元素的结果,因此请尝试将 LEFT JOIN
替换为 INNER JOIN
。
图片来源:http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins
您应该按照您 select 的方式命名您的行变量(就像您对清单 table 的列所做的那样):
错误:
echo "<td>" . $row->Descriptor . "</td>";
echo "<td>" . $row->Type . "</td>";
echo "<td>" . $row->ConditionC . "</td>";
echo "<td>" . $row->Status . "</td>";
echo "<td>" . $row->Location . "</td>";
echo "<td>" . $row->Station . "</td>";
echo "<td>" . $row->cUserFname . "</td>";
echo "<td>" . $row->cUserLname . "</td>";
改用:
echo "<td>" . $row->dename . "</td>";
echo "<td>" . $row->tyname. "</td>";
echo "<td>" . $row->conname. "</td>";
echo "<td>" . $row->statusname. "</td>";
echo "<td>" . $row->locname. "</td>";
echo "<td>" . $row->stationname. "</td>";
echo "<td>" . $row->Fname. "</td>";
echo "<td>" . $row->Lname. "</td>";
好的,所以问题不是将我的辅助 table 上的第三个选项(dename、tyname 等)与库存匹配,而是我应该将它与那个 [=] 的 ID 匹配=14=]。现在,当我使用我的行变量查询第三个选项时,我可以 select 来自我的辅助 table (deid,tyid) 的任何选项。它现在输出所需的结果,希望我可以使用类似的 query.Thanks 为所有想法创建一个下拉列表。