0

Im facing problem in getting data from mysql db by slug url. When i get data from id it works fine. I substituted urlslug also but no use.

Im trying to change my site article url(s)

mysite.com/public.php?id=12    to    mysite.com/public/google-search

My table:

+----+---------------+---------+------------------------------------+
| id | title         | article |   urlslug  VARCHAR 500 NULL        |
+----+---------------+---------+------------------------------------+
| 12 | google search | xxxxxxx |   google-search                    |
| 13 | bing yahoo    | xxxxxxx |   bing-yahoo                       |
| 14 | friendly seo  | xxxxxxx |   friendly-seo                     |
+-------------------------------------------------------------------+

code to get data by 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']);    }

Please help. Thanks.

dan
  • 89
  • 1
  • 1
  • 15
  • You are going to need to parse the url to get 'google-search' and set that in the variable for your db call. Php has a bunch of built in functions to handle that. – VIDesignz Nov 26 '15 at 19:44
  • I want to remove `.php?id=` and want to change to this `mysite.com/public/google-search` I dont know a possible way to get this. Im new to this. – dan Nov 26 '15 at 19:52
  • if your server is `Apache`, then google for `.htaccess` files and `URL Rewriting` – EhsanT Nov 26 '15 at 22:37

2 Answers2

0

Alright, Here you go

When a user goes here....

mysite.com/public/google-search

This rewrite code will change the server side to this

mysite.com/public.php?id=12

By adding this to your .htaccess file

RewriteRule ^public/google-search mysite.com/public.php?id=12 [NC]

You would need to do this for all files that you want to mask.

VIDesignz
  • 4,703
  • 3
  • 25
  • 37
  • I want to remove `.php?id=` and want to change to this `mysite.com/public/google-search` – dan Nov 26 '15 at 19:51
0

You should make your route function, this links may help you: http://blogs.shephertz.com/2014/05/21/how-to-implement-url-routing-in-php/

PHP Application URL Routing

Community
  • 1
  • 1
Gouda Elalfy
  • 6,888
  • 1
  • 26
  • 38