使用 slug url 从 mysql 数据库中获取文章
fetching article from mysql db with slug url
我正在开发自己的 PHP Mysql 网站。
我曾经像这样通过 id 获取数据:mysite.com/articles.php?id=12
现在我想用 slug 更改 url:
mysite.com/articles/google-search
或
mysite.com/articles.php?article=google-search
我不想用id和numbers
我的table:
+----+---------------+---------+------------------------------------+
| id | title | article | urlslug |
+----+---------------+---------+------------------------------------+
| 12 | google search | xxxxxxx | google-search |
| 13 | bing yahoo | xxxxxxx | bing-yahoo |
| 14 | friendly seo | xxxxxxx | friendly-seo |
+-------------------------------------------------------------------+
我使用下面的代码通过 id 获取数据:
$id = $_GET['id'];
$id = mysqli_real_escape_string($conn,$id);
$query = "SELECT * FROM `table` WHERE `id`='" . $id . "'";
$result = mysqli_query($conn,$query);
while($row = mysqli_fetch_array($result)) {
echo ($row['title']);
echo ($row['article']); }
我通过替换 urlslug 来尝试上面的代码,它说 Invalid ID specified.
我用谷歌搜索甚至搜索堆栈问题我没有得到任何帮助。请帮助我。提前致谢。
如果url是这样的mysite.com/articles.php?article=google-search
然后从 url 获取 article
而不是 id
并将条件更改为 urlslug
而不是 id.
$slug = $_GET['article'];
$slug = mysqli_real_escape_string($conn,$slug);
$query = "SELECT * FROM `table` WHERE `urlslug`='" . $slug. "'";
$result = mysqli_query($conn,$query);
//Since slug is unique you will get only 1 result so no need to loop
$row = mysqli_fetch_array($result);
echo $row['title'];
echo $row['article'];
您可以在 Root/.htaccess 中使用以下规则
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^articles/([^/]+)/?$ /articles.php?id= [NC,L]
这将重写
example.com/articles/123
至
example.com/articles.php?id=123
我正在开发自己的 PHP Mysql 网站。
我曾经像这样通过 id 获取数据:mysite.com/articles.php?id=12
现在我想用 slug 更改 url:
mysite.com/articles/google-search
或
mysite.com/articles.php?article=google-search
我不想用id和numbers
我的table:
+----+---------------+---------+------------------------------------+
| id | title | article | urlslug |
+----+---------------+---------+------------------------------------+
| 12 | google search | xxxxxxx | google-search |
| 13 | bing yahoo | xxxxxxx | bing-yahoo |
| 14 | friendly seo | xxxxxxx | friendly-seo |
+-------------------------------------------------------------------+
我使用下面的代码通过 id 获取数据:
$id = $_GET['id'];
$id = mysqli_real_escape_string($conn,$id);
$query = "SELECT * FROM `table` WHERE `id`='" . $id . "'";
$result = mysqli_query($conn,$query);
while($row = mysqli_fetch_array($result)) {
echo ($row['title']);
echo ($row['article']); }
我通过替换 urlslug 来尝试上面的代码,它说 Invalid ID specified.
我用谷歌搜索甚至搜索堆栈问题我没有得到任何帮助。请帮助我。提前致谢。
如果url是这样的mysite.com/articles.php?article=google-search
然后从 url 获取 article
而不是 id
并将条件更改为 urlslug
而不是 id.
$slug = $_GET['article'];
$slug = mysqli_real_escape_string($conn,$slug);
$query = "SELECT * FROM `table` WHERE `urlslug`='" . $slug. "'";
$result = mysqli_query($conn,$query);
//Since slug is unique you will get only 1 result so no need to loop
$row = mysqli_fetch_array($result);
echo $row['title'];
echo $row['article'];
您可以在 Root/.htaccess 中使用以下规则
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^articles/([^/]+)/?$ /articles.php?id= [NC,L]
这将重写
example.com/articles/123
至
example.com/articles.php?id=123