Closed Thread
Results 1 to 7 of 7

Thread: How to Use $_GET in mysql_query() funcion?

  1. #1
    tradingjamie is offline Learning Programmer
    Join Date
    Mar 2009
    Posts
    37
    Rep Power
    0

    How to Use $_GET in mysql_query() funcion?

    Hi All,

    Ive got a page called user.php and when I link to there I get there by sending a URL variable called id.... so basically like this...

    user.php?id=6
    user.php?id=3 etc etc...

    Then in my main code I have a line like this:

    Code:
      <?php
    
    $result = mysql_query("SELECT * FROM sp_table WHERE id=$_GET['id']");
    
    while($row = mysql_fetch_array($result))
      {
      echo $row['name'];
      echo "<br />";
      }
    
    ?>
    For some reason this doesnt work though (i.e. it doesnt display the 'name' field for the given 'id' field).

    I know that the problem part of the code is the $result line because when I replace a plain number instead of the $_GET function it displays the 'name' field ok... i.e. this...:

    Code:
    $result = mysql_query("SELECT * FROM sp_table WHERE id='1'");
    ...would display the correct 'name' field.

    Any ideas what is wrong with my $GET['id'] field?

    Thanks for all your help!

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    tradingjamie is offline Learning Programmer
    Join Date
    Mar 2009
    Posts
    37
    Rep Power
    0

    Re: How to Use $_GET in mysql_query() funcion?

    Ive just figured it out... yay!!!

    I should have added the GET into a variable, and echo the variable, not the funcion...

    i.e.

    Code:
      $idvar = $_GET['id'];
    hope this helps somebody else!

  4. #3
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: How to Use $_GET in mysql_query() funcion?

    I think if you load it directly into the mysql thing you just need to take out the ' character for it to work like this:
    Code:
    $result mysql_query("SELECT * FROM sp_table WHERE id=$_GET[id]"); 
    However you should make it look a bit nicer and filter GET data to prevent injection
    Code:
    $id ereg_replace('[^0-9]'''$_GET['id']);
    $result mysql_query("SELECT * FROM `sp_table` WHERE `id`=$id"); 
    This filters out everything except numbers so that you cant get an injection.

  5. #4
    exile is offline Newbie
    Join Date
    Jul 2009
    Posts
    14
    Rep Power
    0

    Re: How to Use $_GET in mysql_query() funcion?

    or you can use is_numeric() function... it's also good to check, if that id exists with mysql_num_rows()...

    Quote Originally Posted by BlaineSch View Post
    However you should make it look a bit nicer and filter GET data to prevent injection
    Code:
    $id ereg_replace('[^0-9]'''$_GET['id']);
    $result mysql_query("SELECT * FROM `sp_table` WHERE `id`=$id"); 
    This filters out everything except numbers so that you cant get an injection.

  6. #5
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Posts
    3,082
    Blog Entries
    7
    Rep Power
    42

    Re: How to Use $_GET in mysql_query() funcion?

    or, the built in mysql function for preventing injection

    Code:
    $result mysql_query("SELECT * FROM `sp_table` WHERE `id`='".mysql_real_escape_string($_GET['id'])."'"); 
    __________________________________________
    I study Information Systems at Karlstad University when I'm not on CodeCall

  7. #6
    exile is offline Newbie
    Join Date
    Jul 2009
    Posts
    14
    Rep Power
    0

    Re: How to Use $_GET in mysql_query() funcion?

    mysql_real_escape_string() is imo in only-numeric values needless...

  8. #7
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Posts
    3,082
    Blog Entries
    7
    Rep Power
    42

    Re: How to Use $_GET in mysql_query() funcion?

    Well, you can try is_numerical or similar methods first, and throw an error upon that, but the very best advice from me is to always enclose values within the sql statement with apostrophes (') and always escape them at some point.
    __________________________________________
    I study Information Systems at Karlstad University when I'm not on CodeCall

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Beginner Using $_GET
    By Bioshox in forum PHP Tutorials
    Replies: 7
    Last Post: 06-26-2011, 05:36 AM
  2. mysql_query(UPDATE)
    By VakhoQ in forum PHP Development
    Replies: 7
    Last Post: 12-20-2010, 04:43 AM
  3. mysql_query Question
    By SirBee in forum PHP Development
    Replies: 17
    Last Post: 07-19-2010, 08:21 AM
  4. Wrong parameter count for mysql_query() in..
    By gakattack in forum PHP Development
    Replies: 0
    Last Post: 10-18-2009, 02:23 PM
  5. $_POST and $_GET
    By NeedHelp in forum PHP Development
    Replies: 5
    Last Post: 07-17-2006, 04:12 AM

Tags for this Thread

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