-2

Many pages, for example this one, use PHP ID's in the URL (correct me if I'm wrong!) to store and differentiate pages/information. The page I linked has its own ID that refers to the information in the support database that is required. How does someone go about creating that kind of setup? Where is the data for each question stored, in a database?

If I want to create a system where a page has info brought to it depending on a "user ID" such as php?userid=1, what is this process called?

I am not looking for a detailed answer telling me how to do the above process, but more I would like to clear up what I need to learn to achieve it. If I need to explain better please let me know and I'll edit my question so that it can benefit others, not just myself.

k4kuz0
  • 1,045
  • 1
  • 10
  • 24
  • Make sure you know PHP basics first, then get familiar with MySQL and how you interact with it in PHP. Once you're happy with those, and you're comfortable with file inclusion and dynamically creating MySQL queries, look into htaccess rewrite rules. I wouldn't jump into that sort of system without knowing the basics that lead up to it – MDEV Feb 19 '14 at 15:19
  • There is no "process". It's just a query string, and server-side code that reads it and sends some information. – SLaks Feb 19 '14 at 15:21
  • @SLaks Sorry when I say process I mean "means to which I achieve this result". – k4kuz0 Feb 19 '14 at 15:24
  • SO is written in ASP. There is no PHP involved with the site. – Marc B Feb 19 '14 at 15:24
  • @SmokeyPHP I am learning, and understanding, some basic PHP now, but I don't see the link towards writing simply Queries, and the thing I am describing in my question. – k4kuz0 Feb 19 '14 at 15:26
  • @MarcB Updated incorrect info. – k4kuz0 Feb 19 '14 at 15:27
  • Check out my answer on a similar post here: http://stackoverflow.com/a/21714410/3293987 – Quixrick Feb 19 '14 at 15:27
  • @k4kuz0 In simple terms the htaccess file would split the request uri into GET parameters, meaning your PHP file could access the ID with `$_GET['id']`, you would then place this in a MySQL statement that is selecting rows from the `content` table in your database `WHERE id=:id` - you can then use the data returned from the database to build the page. – MDEV Feb 19 '14 at 15:28
  • @SmokeyPHP Thank you for the explanation. It makes more sense now. I understand the simple PHP/MySQL interaction, just not enough to put it into practice the way I wanted. – k4kuz0 Feb 19 '14 at 15:34

2 Answers2

1
  • Yes, the data (here: questions) is stored in a database. It doesn't have to be, but it typically is.
  • Each piece of data has a unique primary id, typically a number. Databases have standard mechanisms to assign such ids, e.g. auto incrementing primary keys in MySQL.
  • In profile.php?user=1, the id 1 is the primary id of that user record in the database.
  • The PHP file fetches the database record based on the ID in the URL.
  • With URL rewriting you can change the appearance of the URL from foo.php?id=42 to /42/something (or any other form you want).
  • You'll have to learn PHP (or any other server side language), a database (MySQL, Postgres, MongoDB, whatever) and URL rewriting (or otherwise configuring your web server of choice to handle URLs the way you want to) to achieve this result.
Community
  • 1
  • 1
deceze
  • 510,633
  • 85
  • 743
  • 889
  • Ah I see, thank you for the explanation. I didn't realise that the ID's in a URL were just the record in a database. I didn't see that link. I am learning basic PHP and MySQL now, however the URL ID part I haven't encountered before at all. – k4kuz0 Feb 19 '14 at 15:30
  • It's really pretty much the basic modus operandi for anything involving PHP and MySQL. Should be one of the first things covered in any PHP+MySQL tutorial. – deceze Feb 19 '14 at 15:31
  • One of the first things? I'm not sure if you're over exaggerating, if I have a terrible PHP tutorial... Or if I'm just a bit slow. Either way, I didn't understand/see the link between regular PHP usage and the thing I was describing! – k4kuz0 Feb 19 '14 at 15:32
  • Probably a bad tutorial then. :) This kind of data retrieval is probably 80% of PHP apps out there (keep in mind that 86% of all statistics are made up on the spot). Any build-a-forum or build-a-blog tutorial explains pretty much this and only this. – deceze Feb 19 '14 at 15:33
  • @k4kuz0 Just my 2 cents here: whilst this interaction between these various parts is important to know if wanting to be a keen PHP developer, I would never say this stuff deserves prime position in a beginners tutorial. Learn the basics, get used to syntax and common functions, then make stuff fancy. If you're releasing something to the public, know it all, if just learning, take your time and cement your understanding with each stage of the process before blending parts together. – MDEV Feb 19 '14 at 15:37
0

You can read these URL variables with the PHP $_GET Super Global. i.e. reading out: ?userid=1 is achieved using:

echo $_GET['userid']; // outputs: 1

More information here

Niek van der Steen
  • 1,413
  • 11
  • 33