简体   繁体   English

在表中存储数据但随机ID检索的最简单数据库解决方案是什么?

[英]What is the simplest Database solution for storing data in tables, but randomised ID retrieval?

I am creating a web based quiz (front end done in HTML and CSS), where the questions will be stored by topic. 我正在创建一个基于Web的测验(在HTML和CSS中完成的前端),问题将按主题存储。 (about 12 or so questions per topic) I want to randomize the selection of the questions within each topic. (每个主题大约12个问题)我想随机选择每个主题中的问题。 Each question will have a picture and audio file. 每个问题都有一个图片和音频文件。 I also want the wrong answers to be randomly chosen from within each topic. 我还希望从每个主题中随机选择错误的答案。

As there will be many topics, I want the table names to be stored as Character Variables/Aliases that can be chosen by the user. 由于将有许多主题,因此我希望表名称存储为可由用户选择的字符变量/别名。 For example, if they choose the Geography button, then the Geography table will be selected. 例如,如果他们选择“地理位置”按钮,则将选择“地理位置”表。
The random ID would be chosen, and the corresponding picture and audio file would be retrieved. 将选择随机ID,并将检索相应的图片和音频文件。 I can then craft one nice looking module and run all of the questions in it. 然后,我可以制作一个外观漂亮的模块并运行其中的所有问题。

I haven't been able to find an example of how this could be done with PHP and MYSQL. 我还没有找到使用PHP和MYSQL如何完成此操作的示例。 From the examples I have seen, the SELECT command may require the actual table name rather than a Character Variable containing the name. 从我看到的示例中,SELECT命令可能需要实际的表名,而不是包含该名称的字符变量。 Also, the way PHP retrieves data seems too convoluted from the examples I have seen. 而且,从我所看到的示例中,PHP检索数据的方式似乎太复杂了。 There seems to be at least 3 steps in the process. 在此过程中,似乎至少需要3个步骤。

Is PHP/MySQL the simplest option, or are there better options that would suit my project with other database systems and languages? PHP / MySQL是最简单的选择,还是有更好的选择适合我的项目以及其他数据库系统和语言?

If anyone could make a snippet of PHP code that would work, I would be very grateful. 如果有人可以编写适用的PHP代码段,我将不胜感激。

PHP and a SQL database (like MySQL, or SQLite, or whatever) is fine for this. PHP和SQL数据库(例如MySQL或SQLite或其他)均适用。 You just need to design the database properly. 您只需要正确设计数据库。

All of the questions are the same type of data, so they shouldn't be split into multiple tables. 所有问题都是同一类型的数据,因此不应将其拆分为多个表。 Use a single table, with the topic as one of the columns in that table; 使用单个表,将主题作为该表中的列之一; you can pick a random question from a specific topic using a query along the lines of: 您可以按照以下方式使用查询从特定主题中选择随机问题:

SELECT ... FROM questions WHERE topic = ? ORDER BY RAND() LIMIT 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM