2016-11-16 68 views
0

我有代码下面的代码工作正常,但我想在这段代码中有一个sql语句而不是少数,所以当我尝试更改为一个我得到一个错误。如何将此select sql语句更改为另一个select sql?

 $eventID = $_GET['id']; 

    $sql = "SELECT * FROM te_events where eventID='$eventID'"; 
    $result = $conn->query($sql); 

    while($row = $result->fetch_assoc()) 
    { 
     $eventTitle = $row['eventTitle']; 
     $eventDescription = $row['eventDescription']; 
     $eventStartDate = $row['eventStartDate']; 
     $eventEndDate = $row['eventEndDate']; 
     $eventPrice = $row['eventPrice']; 
     $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 
$eventID = $_GET['id']; 

    $sql = "SELECT * FROM te_events where eventID='$eventID' AND where venueID='$venueID' From te_venue AND where catID='$catID' From te_category"; 
    $queryresult = mysqli_query($conn, $sql) or die(mysqli_error($conn)); 
    while ($row = mysqli_fetch_array($queryresult)) { 
     $eventTitle = $row['eventTitle']; 
     $eventDescription = $row['eventDescription']; 
     $eventStartDate = $row['eventStartDate']; 
     $eventEndDate = $row['eventEndDate']; 
     $eventPrice = $row['eventPrice']; 
     $venueID = $row['venueID']; 
     $catID = $row['catID']; 
     $catName = $row['catDesc']; 
     $venueName = $row['venueName']; 



    } 

    ?> 

而我得到这个错误。

您的SQL语法错误;检查对应于你的MySQL服务器版本使用附近的正确语法手册“其中venueID =”“从te_venue并在CATID =‘’从te_category”在行1

+0

你不能有多个'from's,也不能有多个'where's。使用连接。您也对SQL注入使用参数化查询开放。这也是非常接近http://stackoverflow.com/questions/40600237/i-have-got-this-code-it-is-working-fine-but-i-want-to-change-the-having- code-int/40600378?noredirect = 1#comment68511702_40600378是某些类的一部分? – chris85

+0

哦,有什么建议吗? –

+0

是的,使用'join'和'参数化查询'。 – chris85

回答

1

是的,你可以这样做。但为了从三张表中获得所需的字段,您必须将它们结合在一起。

看看这篇关于W3S的文章:http://www.w3schools.com/sql/sql_join.asp。它解释了SQL JOIN语法和背后的基本理论。

如果你刚加入地点和事件SELECT语句的样子:

SELECT * FROM te_event 
JOIN te_venue 
ON te_vendue.venueID = te_event.venueID 
WHERE te_event.eventID = $eventID 

类别表是相似的。

注意:一般来说,使用SELECT *是不鼓励的。你应该列出你想从表格返回的字段。即。 SELECT te_eventID,te_venueID

+0

表'.te_event'不存在 –

+0

嘿你的代码正在工作现在只是有拼写错误,只是类别和说明字段我得到错误任何想法? –

0

您可以使用如下连接;

SELECT * FROM te_events 
JOIN te_venue ON te_events.venueID = te_venue.venueID 
JOIN te_category ON te_events.catID = te_category.catID 
WHERE eventID = :EventID 

如前所述,你也应该使用PDO's

define("DB_DSN", "mysql:host=localhost;dbname=foo"); 
define("DB_USERNAME", "root"); 
define("DB_PASSWORD", "password"); 

// define sql 
$sSQL = "SELECT * FROM te_events 
JOIN te_venue ON te_events.venueID = te_venue.venueID 
JOIN te_category ON te_events.catID = te_category.catID 
WHERE eventID = :EventID"; 

// create an instance of the connection 
$conn = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD); 

// prepare 
$st = $conn->prepare($sSQL); 

// securely bind any user input in the query 
$st->bindValue(":EventID", $iEventID, PDO::PARAM_INT); 

// execute the connection 
$st->execute() 

$aResults = array(); 

// loop over results and store in aResults 
while($row = $st->fetch()){ 

    $aResults[] = $row; 

} 

// output data 
foreach($aResults as $aResult){ 
    echo "title: ".$aResult['eventTitle']; 
} 

你应该总是避免使用select *。特别是当你加入时。如果你需要重复的列名,确保你只请求你需要的和别名

+0

我没有得到任何错误与您的第一个代码,但有很多事件,当我点击任何我得到相同的信息,所有假设是不同的每个事件有不同的信息。这是活动页面https://postimg.org/image/fvym719bb/和我获得的所有https:// postimg的信息。org/image/79q14pmm5/ –

+0

难道是你有一个变化,它会显示每一行。您可以在php – atoms

+0

中整合这些多行,这是我得到的https://postimg.org/image/d21mfdru3/ –