2017-05-05 62 views
0

我有工作(见查询结果)暂时取出我没有足够的分两个以上的链路PHP输出到一个表

然而,当我尝试输出在PHP中查询类别名称是对产出的offeredcategory.categoryName和wantedcategory.categoryName既为类别名称为表相同的(见截图):

HTML output

我试图使用别名在查询中输出类别名称不同的提供和想要的。 我也使用$row["offeredcategory.categoryName"]$row["wantedcategory.categoryName"]这将产生一个错误的尝试:

Notice: Undefined index: offeredcategory.categoryName in C:\Program Files (x86)

// Create connection 
    $conn = new mysqli($servername, $username, $password, $dbname); 
    // Check connection 
    if ($conn->connect_error) { 
     die("Connection failed: " . $connection->connect_error); 
    } 

    $sql = "SELECT customers.*, ads.*, categoriesselected.categoryselectedID, categoriesselected.offeredcategoryID, offeredcategory.categoryID, offeredcategory.categoryName, categoriesselected.wantedcategoryID, wantedcategory.categoryID, wantedcategory.categoryName 
    FROM customers 
    INNER JOIN ads ON ads.customerId = customers.customerID 
    INNER JOIN categoriesselected ON categoriesselected.adID = ads.adID 
    LEFT OUTER JOIN categories AS offeredcategory ON offeredcategory.categoryID = categoriesselected.offeredcategoryID 
    LEFT OUTER JOIN categories AS wantedcategory ON wantedcategory.categoryID = categoriesselected.wantedcategoryID"; 
    $result = $conn->query($sql); 

    if ($result->num_rows > 0) { 
     echo "<table> 
       <tr><th></th><th colspan=2>OFFERING</th><th colspan=2>WANTING</th><th>Location</th></tr>"; 
     //need to prevent SQL injection using ... 

     while($row = $result->fetch_assoc()) 
      { 

      echo 
      '<tr> 
       <td><img src="images/'.$row["fileUploadLocation"]. '" width="80" height="80" class="descImage"/></td> 
       <td>' ."<h6>" . $row["categoryName"]. "</h6>" . "<br>" 
         . $row["servicesOfferedTitle"]. '</td> 
       <td>' . $row["servicesOfferedDescription"]. '</td> 
       <td>' . $row["categoryName"]. "<br>" 
         . $row["servicesWantedTitle"]. '</td> 
       <td>' . $row["servicesWantedDescription"]. ' </td> 
       <td>' . $row["location"]. '</td> 
      </tr>'; 
     } 
     echo "</table>"; 
    } else { 
     echo "0 results"; 
    } 

现在我已经试过的建议改变别名从加入到选择,但现在加入将无法工作 有不到$行部分呢。

现在我已经试过的建议改变别名从加入到选择,但现在加入将无法正常工作(见截图):

error 1 using Select alias in join

有没有到$行的部分呢。

从manasschlcatz 接下来尝试第二次建议,但得到的错误:

Notice: Undefined index: offeredName in C:\Program Files (x86)

$sql = "SELECT customers.*, ads.*, categoriesselected.categoryselectedID, categoriesselected.offeredcategoryID, offeredName.categoryID, offeredName.categoryName, categoriesselected.wantedcategoryID, wantedName.categoryID, wantedName.categoryName 
FROM customers 
    INNER JOIN ads ON ads.customerId = customers.customerID 
    INNER JOIN categoriesselected ON categoriesselected.adID = ads.adID 
    LEFT OUTER JOIN categories AS offeredName ON offeredName.categoryID = categoriesselected.offeredcategoryID 
    LEFT OUTER JOIN categories AS wantedName ON wantedName.categoryID = categoriesselected.wantedcategoryID" ; 
    $result = $conn->query($sql); 

if ($result->num_rows > 0) { 
    echo "<table> 
      <tr><th></th><th colspan=2>OFFERING</th><th colspan=2>WANTING</th><th>Location</th></tr>"; 
    //need to prevent SQL injection using ... 

    while($row = $result->fetch_assoc()) 
     { 

     echo 
     '<tr> 
      <td><img src="images/'.$row["fileUploadLocation"]. '" width="80" height="80" class="descImage"/></td> 
      <td>' . $row["offeredName"]. "<br>" 
        . $row["servicesOfferedTitle"]. '</td> 
      <td>' . $row["servicesOfferedDescription"]. '</td> 
      <td>' . $row["wantedName"]. "<br>" 
        . $row["servicesWantedTitle"]. '</td> 
      <td>' . $row["servicesWantedDescription"]. ' </td> 
      <td>' . $row["location"]. '</td> 
     </tr>'; 
    } 
+2

别名它在你的SELECT语句,而不是加入。像“选择offeredcategory.categoryID所提供的”。然后让它像“$ row ['offer']” – StephenCollins

+0

感谢您的帮助。现在遇到连接问题。查看后编辑 –

回答

0

你有重复的字段名称:offeredcategory.categoryNamewantedcategory.categoryName都将被检索$row['categoryName']所以只有一个会出现 - 类别名称是模棱两可但不会被MySQL拒绝,因为SQL语句很清晰,问题在于处理PHP中的结果。简单的解决方案是:

offeredcategory.categoryName as offeredName 
wantedcategory.categoryName as wantedName 

,并根据你真的想显示什么$row['offeredName']$row['wantedName']检索。

完整的SQL变为:

SELECT customers.*, ads.*, categoriesselected.categoryselectedID, categoriesselected.offeredcategoryID, offeredcategory.categoryID, offeredcategory.categoryName as offeredName, categoriesselected.wantedcategoryID, wantedcategory.categoryID, wantedcategory.categoryName as wantedName 
FROM customers 
INNER JOIN ads ON ads.customerId = customers.customerID 
INNER JOIN categoriesselected ON categoriesselected.adID = ads.adID 
LEFT OUTER JOIN categories AS offeredcategory ON offeredcategory.categoryID = categoriesselected.offeredcategoryID 
LEFT OUTER JOIN categories AS wantedcategory ON wantedcategory.categoryID = categoriesselected.wantedcategoryID"; 
+0

查看截图查询工作很好,但检索不起作用我得到注意:未定义的索引:offeredName.categoryName在C:\ Program Files文件(x86)​​'。 $行[ “offeredName”。 “
” –

+0

查询效果很好(LEFT OUTER JOIN categories AS offeredName ON offeredName.categoryID = categoriesselected.offeredcategoryID) 但检索不起作用:我得到了通知:未定义索引:offeredName.categoryName在C:\ Program Files(x86) 。我在html​​'中使用了这个。 $行[ “offeredName”。 “ –

+0

@ Dan-Currie索引只是”as“字段名,而不是表名(隐含的)或原始字段名(不相关)。”未定义索引“应该只发生在数组引用中,而不是SQL语句。没有使用'$ row ['offeredName.categoryName']'错误? – manassehkatz