SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 27
  1. #1
    shauno7's Avatar
    Join Date
    Jul 2004
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to use url string id# to select row from db?

    Hi, I'm new to the backend php mysql development, so if you could explain slowly and clearly, it would be much appreciated.

    In my database, I have each row labelled with an 'ID' number. The question is, how can I pull a string from a url for example:
    www.mydomain.com/index.php?id=3
    where I can then echo all the content in the row with ID 3?

    I know this may seem very basic for some of you, but if you could show me an example it would be great, as I have been looking for tutorials on the internet, and there have been none so far.
    Thanks...

  2. #2
    $this->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Dec 2003
    Location
    Federal Way, Washington (USA)
    Posts
    1,524
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's the code you would need to pull that id into your script:
    Code:
    $id = $_GET['ID'];
    Then I assume you'll have some kind of query against your database similar to this:

    Code:
    SELECT * from tablename where table-id = '$id';
    Hope this helps.
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    Showcase your music collection on the Web

  3. #3
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For the URL :www.mydomain.com/index.php?id=3

    I would add one thing to vinyl-junkie 's code:
    PHP Code:
    // use intval() to avoid SQL injection
    $id intval($_REQUEST['id']);

    $sql "SELECT * from tablename where id = '$id';"

  4. #4
    shauno7's Avatar
    Join Date
    Jul 2004
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, once i selected that row, how do i extract the entries within the row?
    Supposing I have the columns: 'id' , 'title', 'description'.

  5. #5
    SitePoint Wizard Lats's Avatar
    Join Date
    Jun 2003
    Location
    Melbourne, AU
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this...
    PHP Code:
    // use intval() to avoid SQL injection 
    $id intval($_REQUEST['id']); 

    $sql "SELECT * from tablename where id = '$id';"

    $result mysql_fetch_array(mysql_query($sql)) or die(mysql_error());

    echo 
    $result['id'],$result['title'],$result['description']; 
    Lats...

  6. #6
    shauno7's Avatar
    Join Date
    Jul 2004
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow, thanks everyone, it works perfectly. Exactly what I needed.
    Just one more question, what does it mean 'to avoid sql injection'? Is it some security issue?

  7. #7
    SitePoint Wizard Lats's Avatar
    Join Date
    Jun 2003
    Location
    Melbourne, AU
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by shauno7
    ...what does it mean 'to avoid sql injection'? Is it some security issue?
    Yep, check this thread...
    http://www.sitepoint.com/forums/show...threadid=60643
    Lats...

  8. #8
    shauno7's Avatar
    Join Date
    Jul 2004
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    btw, i want to display all the id links on a links page and I want to sort it descendingly. ie. where id5 is on the top of the list and id1 at the bottom.
    Could you show me an example of how to do this.
    This is much appreciated.

  9. #9
    SitePoint Wizard Lats's Avatar
    Join Date
    Jun 2003
    Location
    Melbourne, AU
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something like this...
    PHP Code:
    $sql "SELECT * from tablename order by id desc"
    $sql_result mysql_query($sql)) or die(mysql_error());

    while(
    $result mysql_fetch_array($sql_result); 
    {
        echo 
    "<br>";
        echo 
    $result['id'],$result['title'],$result['description']; 

    Lats...

  10. #10
    SitePoint Evangelist pompopom's Avatar
    Join Date
    Feb 2004
    Location
    Huldenberg (Belgium)
    Posts
    426
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by shauno7
    btw, i want to display all the id links on a links page and I want to sort it descendingly. ie. where id5 is on the top of the list and id1 at the bottom.
    Could you show me an example of how to do this.
    This is much appreciated.
    just a tip:
    all php functions alfabetically ordened (check mysql_ section)
    http://www.php.net/quickref.php

    mysql manual
    http://dev.mysql.com/doc/mysql/en/index.html

    sure they helped me a lot

    greetz,
    koen
    The Path of excess leeds to the tower of wisdom (W. Blake)

  11. #11
    shauno7's Avatar
    Join Date
    Jul 2004
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alright! Thanks you guys. Been of great help. Do you know what other functions would be good if implementing this kind of database driven webpages?

  12. #12
    SitePoint Evangelist pompopom's Avatar
    Join Date
    Feb 2004
    Location
    Huldenberg (Belgium)
    Posts
    426
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by shauno7
    Alright! Thanks you guys. Been of great help. Do you know what other functions would be good if implementing this kind of database driven webpages?
    like I said, check the mysql_section from the php quick reference manual
    (search for all the functions in the list starting with mysql_ (they're ordened alfabetically) )

    when you can't figure out how to achieve your goal: just post a new thread. It's of no use that everybody here starts to give a list of most common or most used functions, as every "goal" has it's own approach and differs from wich function to use best...

    when you don't understand the use of a function you can post it here too

    greetz,
    koen
    The Path of excess leeds to the tower of wisdom (W. Blake)

  13. #13
    $this->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Dec 2003
    Location
    Federal Way, Washington (USA)
    Posts
    1,524
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, you could peruse the php manual for all the different functions that are available (see manual links in prior posts here). However, I'd like to encourage you to consider buying Kevin's Yank's PHP book. In my opinion, it's one of the best learning tools you'll find for building database driven web page content, and in fact is what I used to do just that on my own website.

    Hope this helps.
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    Showcase your music collection on the Web

  14. #14
    SitePoint Evangelist pompopom's Avatar
    Join Date
    Feb 2004
    Location
    Huldenberg (Belgium)
    Posts
    426
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vinyl-junkie
    Well, you could peruse the php manual for all the different functions that are available (see manual links in prior posts here). However, I'd like to encourage you to consider buying Kevin's Yank's PHP book. In my opinion, it's one of the best learning tools you'll find for building database driven web page content, and in fact is what I used to do just that on my own website.

    Hope this helps.
    I forgot to mention that
    vinyl-junkie is right, Kevin's book is a good learning tool indeed
    The Path of excess leeds to the tower of wisdom (W. Blake)

  15. #15
    shauno7's Avatar
    Join Date
    Jul 2004
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, i might get the book, however i'm not that keen on spending too much money. Do any of you know a good website which offers these kind of tutorials?

  16. #16
    $this->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Dec 2003
    Location
    Federal Way, Washington (USA)
    Posts
    1,524
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    Showcase your music collection on the Web

  17. #17
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    some websites do give very good tutorials however, a reference book like Kevin's might seem like alot to spend now but compare that to how much time you will spend trawling the internet for tutorials.

    Spend a little bit now and have a reference and guide in your hands, it will save you more in the long term.
    Just my 2penneth!

    SpikeZ
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  18. #18
    shauno7's Avatar
    Join Date
    Jul 2004
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, thanks. I have managed to select a row from the one id string. Now, the question is, how do I select from multiple variables, supposing I have a column for 'type', 'id', 'title' and 'description'. How would I query a row with a type with 'single' and an 'id' with '3'?

  19. #19
    SitePoint Evangelist pompopom's Avatar
    Join Date
    Feb 2004
    Location
    Huldenberg (Belgium)
    Posts
    426
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    change your MySQL query accordingly

    check http://www.mysql.com

    greetz,
    koen
    The Path of excess leeds to the tower of wisdom (W. Blake)

  20. #20
    SitePoint Wizard Lats's Avatar
    Join Date
    Jun 2003
    Location
    Melbourne, AU
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by shauno7
    ...how do I select from multiple variables, supposing I have a column for 'type', 'id', 'title' and 'description'. How would I query a row with a type with 'single' and an 'id' with '3'?
    Post what you have so far, we can take it from there.
    Lats...

  21. #21
    shauno7's Avatar
    Join Date
    Jul 2004
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    basically i have this:
    // use intval() to avoid SQL injection
    <?php
    $id = intval($_REQUEST['id']);

    $sql = "SELECT * from tablename where id = '$id';";

    $result = mysql_fetch_array(mysql_query($sql)) or die(mysql_error());

    echo $result['id'],$result['title'],$result['description'];
    ?>

    but now, i want to add another column called 'type' so I can sort the rows by the 'type' and 'id' using the url string.

  22. #22
    $this->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Dec 2003
    Location
    Federal Way, Washington (USA)
    Posts
    1,524
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just change this one statement like so:
    Code:
    $sql = "SELECT * from tablename where id = '$id' ORDER BY type, id;";
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    Showcase your music collection on the Web

  23. #23
    SitePoint Wizard Lats's Avatar
    Join Date
    Jun 2003
    Location
    Melbourne, AU
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It doesn't look like much sorting is going to be required, as your sql is pulling out data by ID, usually just 1 record.
    Lats...

  24. #24
    shauno7's Avatar
    Join Date
    Jul 2004
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What happens if I want to pull out a perticular type and a specific id of that type. ie. 'type'=multi and 'id' = 5?
    This is because I have two different contents, single and multi, and I want to pull out specific ids for each one.

  25. #25
    shauno7's Avatar
    Join Date
    Jul 2004
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could someone show me how to accomplish this?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •