我正在尝试编写像 Stackoverflow 这样的网站的代码。不一样。只是算法。
网站上有一个问答系统。我的问题是关于“回答问题”。我怎样才能得到问题的id?问题和答案位于不同的表中。
我可能以错误的方式对网站进行编码。如果我这样做,请告诉我。 注意:我不是母语人士。我在提问时可能会犯一些错误。
最佳答案
In the site, there is a question-answer system. My question is about "answering the question". How can I get the question's id? Questions and answers are in different tables.
我不确定 StackOverflow 是如何编码的,但通常这种类型的事情是使用结构如下的“问题”表来完成的:
id | title | question | .....
然后是引用第一个表中问题的“答案”表,例如
id | question_id | answer | .....
因此,id 为 5、6、22 和 30 的答案都可能引用了 id 为 5 的问题。
要查看对问题 25 的所有回答,例如,您将使用以下命令查询数据库
SELECT answer FROM answers WHERE question_id = 25
<小时/>
更新
要获取问题编号,您可能会从传递到页面的参数中读取它。
例如,问题 25 的 URL 类似于:
www.example.com/showQuestion.php?id=25
在您的代码中,您将执行以下操作:
$questionID = (int)$_GET['id']; // IMPORTANT: use (int) to sanitize input!!!
然后
$result = mysql_query("SELECT answer FROM answers WHERE question_id = ".$questionID);
StackOverflow 使用不同的方式来获取 id,通过执行以下操作:
www.stackoverflow.com/questions/<the id of the question>/<the-title-of-the-question>
例如,此页面是:
http://stackoverflow.com/questions/5282563/get-the-questions-id
引擎可能通过 /
分割地址,然后获取第二个 token ,即 id(在本例中为 5282563)。最后一部分称为 slug .
关于php - 获取问题的id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5282563/