通过主键链接两个表中的多个列
我正在设计一个表格系统,它可以让我创建一个小型的计划表。规划器本身是一个 HTML 表,它利用了两个不同的 MySQL 表。
这些表是:
Table_1 (date_id, suggestion_1, suggestion_2, suggestion_3, ...)
Table_2 (song_id, song_title, ...)
表 1 的“date_id”列填充了日期日历,每个日期都有自己的一组建议。在表 1 的每一行中,建议列的单元格均单独填充从表 2 中的“song_id”列中提取的单个标识号(“1293”、“1185”、“0984”)。
我正在尝试了解如何生成列出日期和建议的 HTML 表,但不只是数字,而是显示第二个表中的歌曲标题。 (还有关于我想要显示的歌曲的其他信息,这就是为什么我有两张表而不是一张表的原因)。
HTML 表最终应如下所示:
HTML_Table (日历日期、歌曲标题 a、歌曲标题 b、歌曲标题 c)
我对 PHP/MySQL 相对较新,(而且是自学的 - 嗯,我有一个书)所以我花了大部分时间来弄清楚我应该如何定义我的问题 - 我现在明白我需要引用第二个表中的主键。我的问题是我该怎么做?我试图弄清楚我是否应该使用某种 JOIN,但我对它们一点也不擅长,尽管我是新的。
我正在使用的书给出了一个针对特定实例的示例函数,我对其进行了修改以供个人使用:
//convert the number of the 1st suggestion into a title
function get_suggest1($suggest_a) {
global $db;
$query = 'SELECT
song_title
FROM
draftsongref
WHERE
song_id = ' . $suggest_a;
$result = mysql_query($query, $db) or die(mysql_error($db));
$row = mysql_fetch_assoc($result);
extract($row);
return $song_title;
}
在下面,我还包括:
//retrieve the suggestions for the mass
$query = 'SELECT
date_id, suggestion_1
FROM
draft_suggestions
ORDER BY
date_id ASC';
$result = mysql_query($query, $db) or die (mysql_error($db));
...
// loop through the results
while ($row = mysql_fetch_assoc($result)) {
extract($row);
$suggest1 = get_suggest1($suggest_a);
...所以我可以使用 $suggest1
变量输出给定建议的特定歌曲标题。我的问题是,每当我运行该函数时,我都会收到一条错误消息。
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 '' at line 6
现在,到目前为止,我已经设法弄清楚它指的是:
WHERE
song_id = ' . $suggest_a;
并且,更具体地说,指的是“= ' . $suggest_a;”部分,但我根本不知道如何解决该特定问题。也许这是我的一个假设(变量参数 $suggest_a 是否有某个名称是否重要?),或者可能是函数设计中的错误 - 当我使用原始示例时,它起作用了,尽管原始参数/变量有没有其他通讯员。
现在,我知道这与我提到的 JOIN 表有相当大的不同,但这是我能够弄清楚如何实现这一点的最接近的表。任何给予的帮助将不胜感激。
I'm working on designing a system of tables that will let me create a small planner of sorts. The planner itself is an HTML table that draws on two different MySQL tables.
The tables are:
Table_1 (date_id, suggestion_1, suggestion_2, suggestion_3, ...)
Table_2 (song_id, song_title, ...)
The "date_id" column of Table 1 is filled with a calendar of, well, dates, each of which has its own group of suggestions. In each row of Table 1, the cells of the suggestion columns are each individually filled with a single identification number ("1293", "1185", "0984") drawn from the "song_id" column in Table 2.
I'm trying to figure out how to generate an HTML table that lists the date and the suggestions, but instead of just numbers, displays the song titles from the second table. (There's other information about the songs that I want to display, too, which is why I've got two tables instead of just 1).
The HTML table should ultimately look like this:
HTML_Table (calendar date, song title a, song title b, song title c)
I'm relatively new to PHP/MySQL, (and self-taught, at that - well, I have a book) so it's taken me most of the day to figure out how I should've defined my issue - I understand now that I need to reference the Primary Key in the second table. My problem is how do I do that? I'm trying to figure out if I should use a JOIN of some kind, but I'm no good at them whatsoever, new as I am.
The book I'm using gave an example function for a specific instance of this, which I modified for my personal use:
//convert the number of the 1st suggestion into a title
function get_suggest1($suggest_a) {
global $db;
$query = 'SELECT
song_title
FROM
draftsongref
WHERE
song_id = ' . $suggest_a;
$result = mysql_query($query, $db) or die(mysql_error($db));
$row = mysql_fetch_assoc($result);
extract($row);
return $song_title;
}
Below that, I also included:
//retrieve the suggestions for the mass
$query = 'SELECT
date_id, suggestion_1
FROM
draft_suggestions
ORDER BY
date_id ASC';
$result = mysql_query($query, $db) or die (mysql_error($db));
...
// loop through the results
while ($row = mysql_fetch_assoc($result)) {
extract($row);
$suggest1 = get_suggest1($suggest_a);
...so I could use the $suggest1
variable to output the specific song_title for the given suggestion. My problem is that, whenever I ran that function, I would receive an error message.
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 '' at line 6
Now, I've so far managed to figure out that it's referring to this:
WHERE
song_id = ' . $suggest_a;
And, more specifically, to the " = ' . $suggest_a;" part, but I simply can't figure out how to fix that particular issue. Maybe its an assumption of mine (does it matter if the variable argument $suggest_a have a certain name?), or maybe its an error in the function's design - when I used the original example, it worked, though the original argument/variable had had no other correspondant.
Now, I understand that's a fair bit different than the JOIN table I mentioned, but that's the closest I've been able to come to figuring out how to aciheve this. Any help granted would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的桌子设计是错误的。该表不应是日期的单行和建议的多列,而是一个日期和一个建议,给定日期的多行。这使您可以对两个表进行简单的连接,以获得每个日期的一组建议以及歌曲标题。
Your table design is wrong. Instead of a single row for a date with multiple columns for suggestions, the table should be a date and a single suggestion, with multiple rows for a given date. This lets you do a simple join of the two tables to get a set of suggestions for each date along with the song title.
好吧,这很尴尬——我已经解决了我自己的问题。本质上,这不是一个函数定义问题,而是一个输出问题。
我错误地使用了定义函数 ($suggest_a) 中的相同参数变量,而不是对任何查询的信息执行任何操作。
相反,我应该将适当的列变量放入该函数的使用中:
它不起作用,因为它没有任何输出。一旦我给它一些东西使用,它就工作得很好。
无论如何,感谢吉姆·加里森(Jim Garrison)提供的有关表结构的提示 - 下次出现此问题时我一定会利用这一点。
Well, this is embarrassing - I've solved my own problem. It wasn't a function definition issue, per se, so much as it was an output issue.
I was mistakenly using the same argument variable from defining the function ($suggest_a) rather than doing anything with any queried information.
I should've instead dropped the proper column's variable into the use of the function:
It didn't work because it had nothing to output. Once I gave it something to use, it worked fine.
In any case, thank you to Jim Garrison for the tips on table structure - I'll be sure to take advantage of that the next time this comes up.