我有2个与此问题相关的表,它们被调用
satellite
和satellite_multi
。我创建表的思维过程是卫星表包含channel_id(主键),频道名称和频道广播的国家,
satellite_multi 表有一个名为channelid_multi的列,即 作为
foreign key
链接到卫星表的channel_id, 和其他列是
SatName,频率,极化,符号, 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的页面,其中显示的频道详细信息基于来自卫星表。例如 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是也显示出来。 (见下图)
我的问题是,我是否需要添加到查询中以检查浏览器链接中的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
感谢任何人提供的任何指导
我已经包含了我的整个`
view_channels.php
`以下代码,以防任何人需要查看
<?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 :(得分: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
`
答案 1 :(得分:0)
让它排序,
这对我有用,感谢指导@Anant和@nowhere
UIImageView