0

I really don't understand what is the actual purpose of a query string.

I can find

I can see links in SO also Use of Query params And I don't know why do wee need this? And how are they using this in the server side to retrieve datas.

Please let me know the use of this.

Note: I can understand we can pass form data through query strings. and Check against backend db to retrieve results.

But i don't understand how do they check page numbers and retrieve the results and some other query strings too.

I hope I am not asking too much and I just want a simple example in the backend processing of page numbers.

Thanks

Community
  • 1
  • 1
Gibbs
  • 21,904
  • 13
  • 74
  • 138
  • sometimes you need to be more specific than just a path when asking the server for a certain page. – dandavis May 12 '15 at 18:11
  • I don't understand this dandavis. Because i have no clue of hoe are these used in the background [i assumed it is used to query db]. But my doubt is how does querying pagnumber return a page. – Gibbs May 12 '15 at 18:12
  • qs doesn't do anything on it's own, it's just advice from the browser to the server about what the client wants to see or do. look at some in the wild to get an idea of how they are used. – dandavis May 12 '15 at 18:13
  • @dandavis So like, I have 50 divs and each div has id[1 to 50]. I am gonna display 5 divs per page. If i pass query param as page=3, it will use this value for calculation. like id's from 11-15 will get displaywd to the user. Is it like that? – Gibbs May 12 '15 at 18:23
  • sure, you could use it for that purpose. – dandavis May 12 '15 at 18:33

2 Answers2

1

The query string not only allows for data to be passed on to dynamic pages, but also it does it in such a way that the user can share the link, so that a different person would get the same page. This is what mainly seperates $_GET from $_POST. POST is more secure and "not in the way" as the user can't see it, but also when ever a page uses POST to retreive data, you can't link to that page in such a way that user B would have the page react in the same way as for the data you might have entered before.

Ben Philipp
  • 1,832
  • 1
  • 14
  • 29
  • I cannot understand your points in a technical manner. Because my question can tell you about my understanding of these things. Thanks. – Gibbs May 12 '15 at 18:19
  • 1
    I'm sure you've often copied the link to a site and pasted it into an email to a friend. Without a query string you couldn't link to a specific video on youtube, for example: instead of just pasting [the direct url to the video](https://www.youtube.com/watch?v=Lg6-Hz0ZtKg), you would have to tell the person to go to youtube and search for "query string php". – Ben Philipp May 12 '15 at 18:23
  • Yes,Yes I get this point. I want to know how `a site like youtube` uses this data in the background. I can understand the purpose – Gibbs May 12 '15 at 18:25
  • Oh. See [http://php.net/manual/en/reserved.variables.get.php](http://php.net/manual/en/reserved.variables.get.php) to learn more about $_GET. The information transmitted consists basically of variables with a name (before the "=") and a value (after the "="). (the value can be omitted). By reading the $_GET array, which contains the variables, you can retreive the information and proceed from there, for example, using PHP. For more inforation as to how to use variables and build logic, refer to any PHP manuals – Ben Philipp May 12 '15 at 18:27
  • I think i am failing to convey my question. Please see the last comment under the question. Is that how the dynamic paging systems work with query strings – Gibbs May 12 '15 at 18:30
  • 1
    It's not about the DOM IDs (the ones you can see in the page source when inspecting a page in your browser) so much as it's all about how the information gets on the page in the first place. Say you have a SQL Database table with 50 entries containing the HTML for those elements. On your serveryou would have a .php script, for example, that looks up all of the entries in that table and loads them into an array. Then, reading the values passed by the query string, it will display only the entries needed for that page. I think you got the basic idea right, but the question was a bit misleading – Ben Philipp May 12 '15 at 18:37
0

Query strings are just a way of passing information from page to server or page to page. It's not a concept just for web, you can use them for any kind of communication from a client to a web based server. An example is an iOS app that connects to a REST server. You might put the Device ID or name or client version number in a query string. When the server receives the call most servers will parse out the query string in a method call and add the values in a method call. Something like this:

https://www.server.com/iOSCall?deviceName=somename&deviceID=1234

could be parsed into a method structure like:

public void iOSCall(string deviceName, string deviceID)

then on the server you can use those variable names and values to do whatever, like logging which device made the call and return a response. A web page is the same way, you might put a data object's ID in the query string so the server knows what you're dealing with (those can be handled with post bodies for calls too, but query strings are easy and quick but not as secure).

So they're basically an easy way for clients to hand data off to servers, servers can handle that data and return a response.

Jake Hargus
  • 352
  • 3
  • 11