2017-05-05 39 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