Jump to content

Trying to understand how to create a dynamic page from a mysql link


Recommended Posts

Hi guys.. 1st post here.  I'm pretty new to php.. just a few weeks in.  I've gotten pretty decent at making mysql connections and extracting data, but now I'm wanting to take one of my urls that I echo to my page and create a new page.  Hoping someone can help...  Here's the picture:


MY MySQL Setup:

I have 1 table called table1. It has 2 columns which are: 'Title' & 'Description'


My Index.php Page:

I connect to mysql. I pull the 'Title' from MySQL and loop it to produce 100 rows of data that fill my index page, as expected.


My Problem:

I now want to be able to click one of those rows and dynamically create a new page (description.php) that includes the 'Title' & 'Description'.


My Progress:

I've managed to click a row and create a hyperlink to mysite.com/$title (where 'title' is pulling the 'title' from the mysql db) but I all I get is page not found.


My Question:

How do I tell description.php to pull the 'Title' and 'Description' and link to it from index.php?


My Gratitude:

Thanks in advance!

Link to comment
Share on other sites

basically this is how it works


in your table you need

id    title    description


then you have mainpage

use php to loop through your table creating links with like this

<a href="articles.php?$row['id']">$row['title']</a>


then your article page is really just a shell

so you will use $_GET['id'] to grab which article was clicked then you can use a while loop using WHERE id=$_GET['id'] and pull that ids title and description thus filling in the shell page with whatever article was clicked.



I hope that helps/makes sense its a very rough explanation and I dont really know how much you already know.


If you want more help just let me know!


Link to comment
Share on other sites

Okay... so this worked perfectly for me.  I am able to create the 'shell' page for my entire database.  Thank you! 


Now that I have the URL, I was just wondering if it was possible to reformat the url of the new page?  Currently, it looks something like: 



For SEO purposes, is there a way to reconstruct it as: 



Link to comment
Share on other sites

Yup, you can put this in the .htaccess file which will be in root directory.


RewriteEngine on
Options +FollowSymLinks
#If base(root) directory is different , change the line below to RewriteBase /directory
RewriteBase /
RewriteRule ^([^/\.]+)/?$ description.php?id=$1 [L]


In your php script:

Description: <?php echo $_REQUEST['id']; ?>


But make sure that mod_rewrite is enabled.


Good luck!

Link to comment
Share on other sites

Use a trailing slash in the URL, this is actually better for SEO than without...



This line should cover that:

RewriteRule ^([^/\.]+)/?$ description.php?id=$1 [L]

/?$ which means that trailing slash is optional, in case some one miss it. ? represents optional. So in this case both URL pattern will work like:



It is because some time we miss the trailing slash, so to avoid that problem, just adding ? will make it optional.

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.