Jump to content

displaying data from mysql on a php page (page.php?ID=)


Recommended Posts

Hello,

I have a database set up which is displaying a single piece of data by looking up the id field which I have entered in, but I have seen on some pages that it is possible to set up a page called, for example, page.php?ID=1 which displays the data from where ID = 1, but I cannot find a tutorial that explains how to do this, could anybody help?

Thank you,

JDRasq

You use $_GET to get url parameters from the url, eg

 

mysite.com?page.php?id=1234

 

to get the id parameter from the url you'd use $_GET['id']. You'd then use $_GET['id'] within your query:

 

// check if id is set and that it only consists of numbers.
// You should always validate data before using it
if(isset($_GET['id']) && is_numeric($_GET['id']))
{
    $id = $_GET['id'];

    $sql = "SELECT * from pages_table WHERE page_id='$id'";

    $result = mysql_query($sql);
   // rest of code here for displaying the page data from the database.
}

One thing to remember is to always validate what come in before making the query. Don't assume that people will only click the links they see. For example what are you going to do if they submit an id beyond those in the database. Maybe common sense to some, but I have made my fair share of mistake this way and sometimes someone gets into what they shouldn't.

 

 

________________

"You cannot escape the responsibility of tomorrow by evading it today." Abraham Lincoln

New Mexico Web Design – Projects (Search food stock imagesMake $$ selling photos)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

Guest
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.