我有代码,下面的代码工作正常,但我希望在此代码中有一个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服务器版本相对应的手册,以便在“场地ID =&#39;&#39;”附近使用正确的语法。来自te_venue和catID =&#39;&#39;来自te_category&#39;在第1行
答案 0 :(得分:1)
是的,你可以这样做。但是为了从所有三个表中获取所需的字段,您必须将它们连接在一起。
看看W3S上的这篇文章:http://www.w3schools.com/sql/sql_join.asp。它解释了SQL JOIN语法和背后的基本理论。
如果您刚加入场地和活动您的选择语句如下:
from tkinter import StringVar
class StringVar_WithHistory(StringVar):
def __init__(self):
self.history = []
def set (self,value):
if StringVar.get(self) != value:
StringVar.set(self, value)
self.history.append(value)
def undo (self):
StringVar.set(self,history[-1])
self.history.pop()
from tkinter import OptionMenu
class OptionMenuUndo(OptionMenu):
def __init__(self,parent,title,*option_tuple,**configs):
self.result = StringVar_WithHistory()
self.result.set(title)
OptionMenu.__init__(self,parent,self.result,*option_tuple,**configs)
def get(self):
return self.result.get()
def undo(self):
self.result.undo()
def simulate_selection(self,option):
self.result.set(option)
类别表类似。
注意:一般情况下,不建议使用 SELECT * 。您应列出要从表中返回的字段。即。 SELECT te_eventID,te_venueID
答案 1 :(得分: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 *。特别是当你加入时。如果需要重复的列名,请确保仅请求所需内容和别名