【问题标题】:How to Assign a URL to Page in php from Database如何从数据库中将 URL 分配给 php 中的页面
【发布时间】:2012-10-14 05:50:04
【问题描述】:

我正在尝试创建类似于博客脚本的内容。

我有一个在 Stackoverflow.com 上找不到的问题

假设我在我的 MySQL 数据库中添加了帖子。而且我也可以取到它们。但是,我如何为他们分配自己唯一的页面 URL,然后获取该特定帖子并将其显示在该页面上。

我希望 URL 与 wordpress 和其他脚本中的 URL 相似。

【问题讨论】:

标签: php


【解决方案1】:

假设在您的数据库中有一个名为“posts”的表,其中包含一个“id”列和一个“title”列等...然后您可以这样进行:

<?php
$q = mysql_query('select id, title from posts') # select posts

while ($row = mysql_fetch_assoc($q)) {
    //dumps article links
    echo sprintf("<a href='mysite.com/article.php?id=%s'>%s</a>", $row['id'], $row['title']);
}
?>
//so, at in article.php

<?php

$article_id = $_GET['id']; //Warning: filter user data

$article = mysql_query("select * from posts where id=$article_id") # select post by id collumn

while ($row = mysql_fetch_assoc($q)) {
    //dump article content...    
}

【讨论】:

  • 这很好。我在问如何制作链接http://www.myurl.com/article_id/http://myurl.com/article_title/
猜你喜欢
  • 1970-01-01
  • 2012-09-02
  • 1970-01-01
  • 2011-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-26
相关资源
最近更新 更多