繁体   English   中英

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

[英]Fetch data from different tables using while loop and if statement

我试图使用8个不同的表从数据库中获取数据:

inventory descriptorid typeid conditionid statusid locationid stationid users

我相信我的查询工作正常,问题是使用我的$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>";

您确定要使用LEFT JOIN而不是INNER JOIN吗? 这意味着,如果您拥有表A和B,则仅采用位于A中但不一定位于B中的元素。这意味着,如果左侧表中的元素没有匹配的伙伴,则将有一行包含一个元素一列中表A的null ,另一列中为null 这可能是导致结果集仅包含inventory表元素的原因之一。 据我了解,您想要一个包含在相等属性上连接的元素的结果,因此请尝试将LEFT JOIN替换为INNER JOIN

SQL联接

图片来源: http : //www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins

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

错误:

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

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

暂无
暂无

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

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