![](/img/trans.png)
[英]Display rows with a specific ID from all tables in a database in PHP mysql
[英]mysql/php - view specific rows from database table based on id
我有2个与此问题相关的表,它们称为satellite
和satellite_multi
。 我创建表格的过程是,卫星表格包含channel_id(主键),频道名称和频道从哪个国家/地区播出,
satellite_multi表有一个称为channelid_multi的列,该列作为
foreign key
链接到Satellite表中的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的页面,其中根据卫星表中频道的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();
输出是针对每个频道ID显示来自satellite_multi表和Satellite表的所有信息,在此示例中,由于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';
?>
就像其他人已经在注释中告诉您的一样,查询的问题是在JOIN
语句之前使用WHERE
条件,这是错误的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
`
整理好了
这对我有用,感谢@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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.