简体   繁体   English

得到问题的ID

[英]Get the question's id

I'm trying to code a site like Stackoverflow. 我正在尝试编写像Stackoverflow这样的网站。 Not the same. 不一样。 Just the algorithm. 只是算法。

In the site, there is a question-answer system. 在网站上,有一个问答系统。 My question is about "answering the question". 我的问题是“回答这个问题”。 How can I get the question's id? 我怎样才能得到问题的ID? Questions and answers are in different tables. 问题和答案在不同的表格中。

I might be coding the site in the wrong way. 我可能会以错误的方式编写网站代码。 If I do that, please tell me. 如果我这样做,请告诉我。 Note: I'm not a native speaker. 注意:我不是母语人士。 I can made some mistakes while I'm asking. 我问的时候我会犯一些错误。

In the site, there is a question-answer system. 在网站上,有一个问答系统。 My question is about "answering the question". 我的问题是“回答这个问题”。 How can I get the question's id? 我怎样才能得到问题的ID? Questions and answers are in different tables. 问题和答案在不同的表格中。

I am not sure of how StackOverflow is coded, but usually this type of thing is done using a "questions" table with structure like: 我不确定StackOverflow是如何编码的,但通常这种类型的事情是使用“问题”表完成的,其结构如下:

id   |   title   |    question  | .....

and then an "answers" table that references the questions in the first table, such as 然后是一个“答案”表,引用第一个表中的问题,例如

id   |   question_id  |  answer  | .....

So, the answers with id 5, 6, 22 and 30 may all reference the question with id 5. 因此,id为5,6,22和30的答案都可以引用id为5的问题。

To see all responses to question 25, for instance you will then query the DB using 要查看对问题25的所有回复,例如,您将使用查询数据库

SELECT answer FROM answers WHERE question_id = 25

Update 更新

To get the question number you'll probably read it from the parameters passed to the page. 要获取问题编号,您可能会从传递给页面的参数中读取它。

For instance, the URL for question 25 will be something like: 例如,问题25的URL将类似于:

www.example.com/showQuestion.php?id=25

In your code you will do: 在您的代码中,您将:

$questionID = (int)$_GET['id']; // IMPORTANT: use (int) to sanitize input!!!

And then 接着

$result = mysql_query("SELECT answer FROM answers WHERE question_id = ".$questionID);

StackOverflow uses a different way to get the id, by doing: StackOverflow使用不同的方式来获取id,方法是:

www.stackoverflow.com/questions/<the id of the question>/<the-title-of-the-question>

So, for instance, this page is: 所以,例如,这个页面是:

http://stackoverflow.com/questions/5282563/get-the-questions-id

The engine probably splits the address by / and then gets the 2nd token which is the id (5282563 in this case). 引擎可能会将地址拆分为/ ,然后获取第二个令牌,即id(本例中为5282563)。 The last part is called a slug . 最后一部分被称为slug

If you meant "get the question by the id", then it should be something like: 如果你的意思是“通过id获取问题”,那么它应该是这样的:

SELECT question_id, title, body FROM question WHERE question_id = the_id

Assuming that your question has id , title and body , and: 假设你的问题有idtitlebody ,并且:

  • your questions table is called question 你的问题表叫做question
  • the_id is the id of the question you're trying to fetch the_id是您尝试获取的问题的ID

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

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