我有代码下面的代码工作正常,但我想在这段代码中有一个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
你不能有多个'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
哦,有什么建议吗? –
是的,使用'join'和'参数化查询'。 – chris85