Php 使用while循环和if语句从不同的表中获取数据

Php 使用while循环和if语句从不同的表中获取数据,php,mysql,while-loop,fetch,Php,Mysql,While Loop,Fetch,我正在尝试使用8个不同的表从数据库中获取数据,这些表名为: 库存描述符rid typeid conditionid statusid locationid stationid用户 我相信我的查询工作正常。问题是使用了我的$row变量。我只是从库存表中的数据中得到结果。有人能解释一下我如何从每个表中提取数据吗 代码如下: <?php // get the records from the database if ($result = $con->query("

我正在尝试使用8个不同的表从数据库中获取数据,这些表名为:

库存描述符rid typeid conditionid statusid locationid stationid用户

我相信我的查询工作正常。问题是使用了我的
$row
变量。我只是从库存表中的数据中得到结果。有人能解释一下我如何从每个表中提取数据吗

代码如下:

<?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>";

您确定要使用
左联接
而不是
内联接
?这意味着,如果有表A和表B,则只获取位于A中但不一定位于B中的元素。这意味着,如果左表的某个元素没有匹配的伙伴,则将有一行在一列中包含表A的元素,在另一列中包含
null
。 这可能是resultset只包含
inventory
表的元素的原因之一。 据我所知,您希望得到一个包含在相等属性上连接的元素的结果,因此请尝试将
左连接
替换为
内部连接


图像来源:

您应该在选择行变量时命名它们(就像您在清单表的列中所做的那样):

错:

        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”“$行->描述符。"";
“回声”$行->类型。"";
“回声”$行->条件C。"";
“回声”$行->状态。"";
“回声”$行->位置。"";
“回声”$行->站。"";
“回声”$行->客户名称。"";
“回声”$行->客户名称。"";
改用:

        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>";
echo”“$排->德纳姆。"";
“回声”$行->名称。"";
“回声”$行->连接名。"";
“回声”$行->状态名称。"";
“回声”$行->本地名称。"";
“回声”$行->站名。"";
“回声”$行->Fname。"";
“回声”$行->名称。"";

好的,所以问题不是将我的辅助表上的第三个选项(dename、tyname等)与库存匹配,而是我应该将其与该表的ID匹配。现在,当我用我的行变量查询第三个选项时,我可以从我的辅助表(deid,tyid)中选择任何选项。它现在输出所需的结果,希望我能使用类似的查询创建一个下拉列表。感谢所有的想法

这是不对的。左连接意味着即使表B中没有对应的行,它也会接受表A中的行,但如果有,您也会得到表B的结果(除非您用“WHERE B.Key is NULL”排除它们,这是您优秀的图表中下面的一行)。很抱歉我的误会,据我所知,它根据您在
on
子句中指定的内容连接两个表,然后仅获取位于右侧的元素。这是否意味着结果中有完整的表A?在这种情况下,没有WHERE子句:是的,你是对的,我现在在自己的图像中识别出了它。我真丢脸。但我认为在他的例子中,
内部连接将起作用,不是吗?
LEFT JOIN
只会产生一个结果,其中包含
inventory
中的informatik,如果右侧没有,是否正确?我将更新我的答案。我正在尝试使用清单表中的id连接到其他表中的数据。我不希望保留从两个表中获取的数据,只保留descriptorid、typeid等中的ID。如何在库存中替换该ID以匹配我要加入它的表的ID和名称?