2016-11-15 51 views
1

這是我在工作正常的那一刻代碼:我有這個代碼是工作正常,但我想將其代碼轉變爲另一種結構

<?php 
    $sql = "SELECT * FROM te_events order by eventTitle ASC "; 
    $result = $conn->query($sql); 

    while($row = $result->fetch_assoc()) 
    { 
     $venueID = $row['venueID']; 
     $catID = $row['catID']; 

     $sql2 = "SELECT * FROM te_venue where venueID='$venueID'"; 
     $result2 = $conn->query($sql2); 

     while($row2 = $result2->fetch_assoc()) 
     { 
      $venueName = $row2['venueName']; 
     } 

     $sql3 = "SELECT * FROM te_category where catID='$catID'"; 
     $result3 = $conn->query($sql3); 

     while($row3 = $result3->fetch_assoc()) 
     { 
      $catName = $row3['catDesc']; 
     } 

?> 

但我想將其更改爲這種格式。我只能做到這一點不能超過這個我得到的錯誤。

<?php 
    $sql ="SELECT eventTitle, eventID, venueID, catID, eventStartDate, eventEndDate, eventPrice FROM te_events ORDER BY eventTitle ASC"; 
    $queryresult = mysqli_query($conn, $sql) or die(mysqli_error($conn)); 
    while ($row = mysqli_fetch_array($queryresult)) { 

     $venueID = $row['venueID']; 
     $catID = $row['catID']; 
     $venueName = $row['venueName']; 
     $catName = $row['catDesc']; 

?> 

我該怎麼做呢?
我該如何加入兩張桌子?

+0

我該怎麼做呢? –

+0

我如何加入兩張桌子? –

+0

沒有代碼的問題對未來的訪問者無效。請保留問題中的代碼。如果有一些安全數據(我沒有看到),則應該在服務器上重置它,因爲它已經被暴露。 – chris85

回答

0

您應該可以通過join附加2個表來獲取所需的列。

SELECT e.eventTitle, e.eventID, e.venueID, e.catID, e.eventStartDate, e.eventEndDate, e.eventPrice, v.venueName, c.catDesc 
FROM te_events as e 
join te_venue as v 
on e.venueID = v.venueID 
join te_category as c 
on c.catID = e.catID 
ORDER BY eventTitle ASC 

您還應該避免將數據直接放入查詢。如果您需要使用參數化查詢。這是如何發生SQL注入(或第二級)。

+0

它沒有工作對不起... –

+0

發生了什麼?可能需要在'order by'中爲'eventTitle'提供別名,這是一個獨特的名字嗎? – chris85

+0

哦,我使用了錯誤的別名。你知道這個代碼在做什麼嗎?在插入之前,你應該真正理解一些東西。'e','v'和'c'是你表格的別名。 – chris85

相關問題