2016-07-25 43 views
0

我有2个与此问题相关的表,它们被称为 satellitesatellite_multi。我为创建表的思维过程是卫星工作台中包括信道的CHANNEL_ID(主键),姓名及国指从通道播放,mysql/php - 基于ID查看数据库表中的特定行

satellite_multi table has a column called channelid_multi, which is linked as a foreign key to the channel_id from the satellite table, and the other columns are
SatName, Frequency, Polarisation, Symbrate, FEC, EncorFTA.

我创建了satellite_multi表,因为有一些多个卫星上的信道,例如Thor 0.8w和Hotbird 13.0e可能具有相同的频道广播,所以我需要一种能够显示多行数据的方式,如果频道在多个卫星上广播的话。

下面是satellite_multi表的表结构:

+-----------------+---------------+-----------+--------------+------------+-----+----------+ 
| ChannelID_Multi(FK) | SatName | Frequency | Polarisation | Symbrate | FEC | EncorFta | 
+-----------------+---------------+-----------+--------------+------------+-----+----------+ 
|    1 | Thor 0.8w  |  10932 | H   |  275000 | 5/6 | ENC | 
|    1 | Hotbird 13.0e |  10654 | V   |  25000 | 3/4 | FTA | 
+-----------------+---------------+-----------+--------------+------------+-----+----------+ 

This is the table structure for the table named satellite: 
+-----------+----------------+----------+ 
| ChannelID (PK) |  Name  | Country | 
+-----------+----------------+----------+ 
|   1 | Polsat Sport | Poland | 
|   2 | Sky Sports | England | 
+-----------+----------------+----------+ 

我有站点设置在用户点击的主要网站上的频道名称的超链接,他们被带到了一个名为页面view_channels .php,其中通道详细信息基于来自卫星表的频道ID显示。例如view_channel.php?channelid = 19

当通道位于一颗卫星上时,此功能正常工作,因为我可以运行SELECT *查询并显示所有数据。

我试图让多个通道数据显示在每个单独的通道ID下,但可悲的是它不起作用。

我用下面的下面的代码在我view_channels.php页

$sql = "SELECT s.name, s.country, f.satname, f.frequency, f.polarisation, f.symbrate, f.fec, f.encorfta 
FROM satellitemulti f 
LEFT JOIN satellite s 
ON f.channelid_multi=s.channelid 
GROUP BY s.name, s.country, f.satname, f.frequency, f.polarisation, f.symbrate, f.fec, f.encorfta"; 
$stmt = $DB->prepare($sql); 
$stmt->execute(); 

的输出被示为全部来自satellite_multi表和卫星表中的信息为每一个信道ID,在该例子中,作为Polsat是ID 1,只显示polsat,但也显示包含不同ID的AFN Sports。 (见下图)

All Data showing

我的问题是,是不是我要添加到我的查询,以检查从浏览器链接的ID,并从表中收到的ID匹配,所以只有特定ID的频道数据才会显示?

我试图在添加一个WHERE子句,以显示基于channelid_multi

WHERE channelid_multi = $channelid_multi 

数据,但我得到了一个错误:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'JOIN satellite s ON f.channelid_multi=s.channelid GROUP BY s.name, s.country, f.' at line 4' in E:\home\students\2132\B00614408\public_html\sportsschedule\view_channels.php:19 Stack trace: #0 E:\home\students\2132\B00614408\public_html\sportsschedule\view_channels.php(19): PDOStatement->execute() #1 {main} thrown in E:\home\students\2132\B00614408\public_html\sportsschedule\view_channels.php on line 19 

感谢您的任何指导,任何人都可以提供

我已包括我的整个`

0下面

`代码的情况下,任何人都需要看那个

<?php 

require_once './config.php'; 
include './header.php'; 

$sql = "SELECT s.name, s.country, f.satname, f.frequency, f.polarisation, f.symbrate, f.fec, f.encorfta 
FROM satellitemulti f 
WHERE channelid_multi = $channelid_multi 
LEFT JOIN satellite s 
ON f.channelid_multi=s.channelid 
GROUP BY s.name, s.country, f.satname, f.frequency, f.polarisation, f.symbrate, f.fec, f.encorfta"; 
$stmt = $DB->prepare($sql); 
$stmt->execute(); 


?> 




<div class="panel panel-primary"> 
<div class="panel-heading"> 
<h3 class="panel-title"> Whats On</h3> 
</div> 
<div class="panel-body"> 
    </div> 


<div class="clearfix"></div> 
<div class="table-responsive"> 
<table class="table table-striped table-hover table-bordered "> 
<tbody> 
<caption> Channel Details</caption> 
<tr> 
<th>Name</th> 
<th>Country</th> 
<th>Sat Name</th> 
<th>Frequency</th> 
<th>Polarisation</th> 
<th>Symbol Rate</th> 
<th>FEC</th> 
<th>Enc or FTA</th> 

</tr> 
<?php while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) 
{ 

$name = $row['name']; 
$country= $row['country']; 
$satname = $row['satname']; 
$frequency=$row['frequency']; 
$polarisation=$row['polarisation']; 
$symbrate=$row['symbrate']; 
$fec=$row['fec']; 
$encorfta=$row['encorfta']; 
$channelid_multi=$row['channelid_multi']; 



echo "<tr>"; 
echo "<td>" . $row['name'] . "</td>"; 
echo "<td>" . $row['country'] . "</td>"; 
echo "<td>" . $row['satname'] . "</td>"; 
echo "<td>" . $row['frequency'] . "</td>"; 
echo "<td>" . $row['polarisation'] . "</td>"; 
echo "<td>" . $row['symbrate'] . "</td>"; 
echo "<td>" . $row['fec'] . "</td>"; 
echo "<td>" . $row['encorfta'] . "</td>"; 

} 
echo "</tr>"; 
echo "</table>"; 
?> 
</div> 


<?php 
include './footer.php'; 
?> 
+0

'$ sql =“SELECT s.name,s.country,f.satname,f.frequency,f.polarisation,f.symbrate,f.fec,f.encorfta FROM satellitemulti f LEFT JOIN卫星s ON f.channelid_multi = s.channelid WHERE channelid_multi = $ channelid_multi GROUP BY s.name,s.country,f.satname,f.frequency,f.polarisation,f.symbrate,f.fec,f.encorfta“ ;' –

+0

是的,在哪里需要去左后加入和工作。 – nowhere

+0

感谢您的帮助。我试着改变了上面所说的查询,但是我得到这个错误。 –

回答

0

与其他人一样已经告诉过你的意见与你 查询的问题是,你使用WHERE条件JOIN语句之前和那 是错误的mysql语法。 所以你必须先改变你这样的查询工作:

$sql = "SELECT s.name, s.country, f.satname, f.frequency, f.polarisation, f.symbrate, f.fec, f.encorfta 
FROM satellitemulti f 
LEFT JOIN satellite s 
ON f.channelid_multi=s.channelid 
WHERE channelid_multi = $channelid_multi 
GROUP BY s.name, s.country, f.satname, f.frequency, f.polarisation, f.symbrate, f.fec, f.encorfta"; 

你有没有良好的数据库设计。

例如当卫星名称发生变化时会发生什么?您必须更新具有此卫星的 satellite_multi中的每一行。

既然你有一个多一对多的关系,我会用3个表。

  • 一个名为satellites的卫星。

  • 一个名为channels

  • 频道和许多一对多表命名为channels2satellites

    通知:我假设频率,极化等是卫星的属性。如果他们是一个频道的属性,只需将它们移动到channels表。

satellites

+-----------------+---------------+-----------+--------------+------------+-----+----------+ 
|   ID(PK) | SatName | Frequency | Polarisation | Symbrate | FEC | EncorFta | 
+-----------------+---------------+-----------+--------------+------------+-----+----------+ 
|    1 | Thor 0.8w  |  10932 | H   |  275000 | 5/6 | ENC | 
|    2 | Hotbird 13.0e |  10654 | V   |  25000 | 3/4 | FTA | 
+-----------------+---------------+-----------+--------------+------------+-----+----------+ 

channels

+-----------+----------------+----------------+ 
|  ID (PK) |  Name  | Country | 
+-----------+----------------+----------+ 
|   1  | Polsat Sport | Poland | 
|   2  | Sky Sports | England | 
+-----------+----------------+----------------+ 

channels2satellites

+-----------+----------------+----------------------------+ 
|  ID (PK) | channel_id(FK) | satellite_id(FK) | 
+----------------+--------------------+-------------------+ 
|   1  | 1    | 1    | 
|   2  | 1    | 2    | 
|   3  | 2    | 1    | 
+-----------+----------------+----------------------------+ 

当我需要的数据为通道我会使用这个查询。 比方说,你想为频道1

SELECT c.Name,c.Country,s.SatName,s.Frequency,s.Polarization.s.Symbrate,s.FEC,s.EncorfFta FROM channels c INNER JOIN channels2satellites c2s ON c.id=c2s.channel_id INNER JOIN satellites s ON c2s.satellite_id=s.id WHERE c.id=1

`

+0

谢谢,我将更改我的数据库结构以匹配此数据库,因为这是一个更好的数据库设计 –

0

得到它排序信息,

这是对我工作,感谢指导@Anant和@nowhere

$channelid = $_GET["channelid"]; 
$sql = "SELECT s.name, s.country, f.satname, f.frequency, f.polarisation, f.symbrate, f.fec, f.encorfta 
FROM satellitemulti f 
LEFT JOIN satellite s 
ON f.channelid_multi=s.channelid 
WHERE channelid = :channelid 
GROUP BY s.name, s.country, f.satname, f.frequency, f.polarisation, f.symbrate, f.fec, f.encorfta"; 
$stmt = $DB->prepare($sql); 
$stmt -> bindParam(':channelid', $channelid); 
$stmt->execute();