Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > MySQL + PHP: how use url string id# to select row from DB?

MySQL + PHP: how use url string id# to select row from DB?
Thread Tools
Dedicated MacNNer
Join Date: Jun 2001
Location: Xanadu Roller Palace, Chicago
Status: Offline
Reply With Quote
Nov 6, 2004, 02:21 PM
 
Hi. I'm very new to this so please speak slowly and loudly. I have each row in my DB labeled with an id #. There is a column called "id" and a single digit denoting that row. How can i pull the string from the url
Code:
http://url.com/page.php?id=2
and have it echo the content only from that row with the id of "2"?


Right now i have a database set up with one table for each section of the site I'm building. Each sub-section has an id# associated with it. So for example i have the first section "Company" and sections within "Company": "Overview, Management, etc."

"Company" is the first table and the subsections have the id#'s. ie. Overview id = 1, Management id = 2, etc. Ultimately i want to just have a url like:
Code:
http://url.com/company.php?id=2
which will echo the content for the Management row. I have everything working great, the DB, i know how to get the info from the DB to be on the PHP page in the browser and all that good stuff.

I know this is done all the time but would love to see an example that i can repurpose. I didn't really know what to search for on the web to find a tutorial.

Thanks!
     
Mac Elite
Join Date: Oct 1999
Location: San Jose, Ca
Status: Offline
Reply With Quote
Nov 6, 2004, 09:18 PM
 
If I am understanding you correctly... you should not be using a database for this information... either your site is small enough that you should be doing all the pages by hand, or it is large enough that you should be using one of the many CMS's out there.

On the learning part... you really do have to learn how to do something in order to do it. There is a reason people go through tutorials.
     
Dedicated MacNNer
Join Date: Jun 2001
Location: Xanadu Roller Palace, Chicago
Status: Offline
Reply With Quote
Nov 6, 2004, 09:34 PM
 
Originally posted by larkost:
If I am understanding you correctly... you should not be using a database for this information... either your site is small enough that you should be doing all the pages by hand, or it is large enough that you should be using one of the many CMS's out there.

On the learning part... you really do have to learn how to do something in order to do it. There is a reason people go through tutorials.
I got the answer. It was adding a WHERE to the select command
Code:
"SELECT * FROM $page_file WHERE sub = '$section' && id = '$pageid' "
sub and id are passed along the url.

Thanks for the advice but i'm building my own CMS and the site is large enough that is just faster to build one template in PHP and pull all the content from the database.

Everything works great now. Maybe it will help someone else.
     
Grizzled Veteran
Join Date: Jun 2001
Location: Melbourne, Australia
Status: Offline
Reply With Quote
Nov 7, 2004, 04:16 PM
 
Don't forget to sanitise your request variables before you go plonking them in your SQL, otherwise something nasty may happen... you never know what people will go sticking in their URLs for perverse pleasure!

If your pageid is an integer, then simply performing a $pageid = (INT) $pageid will protect that - if your $section is a string then perhaps a $section = preg_replace('/[^\w_]+/msi','',$section) will suffice to strip out nasties.

I must concur with Larkost on the tutorial front, though. It's like driving in 1st gear with a 10 year old road map if you rely purely on existing examples and code snippets alone. I bought one of the MySQL in 24 hour books from SAMS and it was a great start for a very small investment.
Computer thez nohhh...
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:19 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2