PHP Get Help

Discussion in 'PHP' started by Pixel T., Jan 2, 2010.

  1. #1
    Hey guys,

    I'm back. I'm stuck on something really simple if you know what you are doing.

    I made a page called page.php and I want it to display stuff from a mySQL database. I wanted to use GET_ to make it so if someone types in page.php?id=coke and "coke" is in my database, then the page would display everything else that is in the same row as coke.

    Not sure if that made any sense. Heres what I tried but failed at.

    $result = mysql_query("SELECT * FROM projects ");
    while($row = mysql_fetch_array($result))
    {
        $id = $_GET['id'];
        $num = $row['id'];
                if($id == $num)
                {
                echo $row['client']." is very important";
                }
                else
                {
                echo "Not found";
                }
    }
    Code (markup):
    Anything will help. Thanks guys
     
    Pixel T., Jan 2, 2010 IP
  2. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #2
    $result = mysql_query("SELECT * FROM projects where client = '".mysql_escape_string($_GET['id'])."' ");
     
    crivion, Jan 2, 2010 IP
  3. Pixel T.

    Pixel T. Well-Known Member

    Messages:
    1,205
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    170
    #3
    Do I need to take away anything from my code besides that one line? When I switch the line to yours, it doesn't give me the data. Its just blank.

    Thanks,
     
    Pixel T., Jan 2, 2010 IP
  4. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #4
    after the $result = line put
    if(!$result) die(mysql_error());
    and that will give you the reason the query isnt working
    Also, have you connected to mysql and selected a database?
     
    JAY6390, Jan 2, 2010 IP
  5. CodedCaffeine

    CodedCaffeine Peon

    Messages:
    130
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    This should work for what you need :)

    # Filter everything but numbers, plus, and minus.
    $id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT);
    
    # Escape the ID to prevent issues, and transform it into an int (if it's not)
    $sql = sprintf('SELECT * FROM `projects` WHERE `id` = '%d' LIMIT 0, 1;',
    		mysql_real_escape_string((int)$id));
    
    # Create the result.
    $result = mysql_query($sql);
    
    # If the result had an entry..
    if(num_rows($result) > 0)
    {
    	# Fetch the row.
    	$row = mysql_fetch_row($result);
    	
    	# Echo the client and string.
    	echo $row['client'].' is very imporant';
    }
    else
    {
    	# The result didn't have an entry
    	echo 'Not found';
    }
    PHP:
     
    Last edited: Jan 2, 2010
    CodedCaffeine, Jan 2, 2010 IP
  6. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #6
    in the above code it should be sprintf not sprint_f
     
    JAY6390, Jan 2, 2010 IP
  7. CodedCaffeine

    CodedCaffeine Peon

    Messages:
    130
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Whoops! Thanks. I've been used to writing print_r for a while so it was a natural keystroke. haha.
     
    CodedCaffeine, Jan 2, 2010 IP
  8. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #8
    hehe happens to us all
     
    JAY6390, Jan 2, 2010 IP
  9. Pixel T.

    Pixel T. Well-Known Member

    Messages:
    1,205
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    170
    #9
    Thanks for the help so far guys but I'm getting this error
     
    Pixel T., Jan 2, 2010 IP
  10. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #10
    # Filter everything but numbers, plus, and minus.
    $id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT);
    
    # Escape the ID to prevent issues, and transform it into an int (if it's not)
    $sql = sprintf("SELECT * FROM `projects` WHERE `id` = '%d' LIMIT 0, 1;",
            mysql_real_escape_string((int)$id));
    
    # Create the result.
    $result = mysql_query($sql);
    
    # If the result had an entry..
    if(num_rows($result) > 0)
    {
        # Fetch the row.
        $row = mysql_fetch_row($result);
        
        # Echo the client and string.
        echo $row['client'].' is very imporant';
    }
    else
    {
        # The result didn't have an entry
        echo 'Not found';
    }
    PHP:
    Try that
     
    JAY6390, Jan 2, 2010 IP
    Pixel T. likes this.
  11. Pixel T.

    Pixel T. Well-Known Member

    Messages:
    1,205
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    170
    #11
    Thats if(num_rows($result) > 0)

    Thanks,
     
    Pixel T., Jan 2, 2010 IP
  12. CodedCaffeine

    CodedCaffeine Peon

    Messages:
    130
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Typos are annoying.. lol. This last one should work.

    # Filter everything but numbers, plus, and minus.
    $id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT);
    
    # Escape the ID to prevent issues, and transform it into an int (if it's not)
    $sql = sprintf("SELECT * FROM `projects` WHERE `id` = '%d' LIMIT 0, 1;",
            mysql_real_escape_string((int)$id));
    
    # Create the result.
    $result = mysql_query($sql);
    
    # If the result had an entry..
    if(mysql_num_rows($result) > 0)
    {
        # Fetch the row.
       $row = mysql_fetch_row($result);
       
        # Echo the client and string.
       echo $row['client'].' is very imporant';
    }
    else
    {
        # The result didn't have an entry
       echo 'Not found';
    }
    PHP:
     
    CodedCaffeine, Jan 2, 2010 IP
  13. Pixel T.

    Pixel T. Well-Known Member

    Messages:
    1,205
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    170
    #13
    No errors but its not retreving any data. All I get is "is very important".

    Hmm...

    Would the problem be with the ' and " in the row? You used ' for both.

    I tried what I could but still nothing
     
    Pixel T., Jan 2, 2010 IP
  14. CodedCaffeine

    CodedCaffeine Peon

    Messages:
    130
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #14
    I need to start reading up on stuff again. lol.

    This is the correct code for sure:

    # Filter everything but numbers, plus, and minus.
    $id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT);
    
    # Escape the ID to prevent issues, and transform it into an int (if it's not)
    $sql = sprintf("SELECT * FROM `projects` WHERE `id` = '%d' LIMIT 0, 1;",
            mysql_real_escape_string((int)$id));
    
    # Create the result.
    $result = mysql_query($sql);
    
    # If the result had an entry..
    if(mysql_num_rows($result) > 0)
    {
        # Fetch the row.
      $row = mysql_fetch_assoc($result);
       
        # Echo the client and string.
      echo $row['client'].' is very imporant';
    }
    else
    {
        # The result didn't have an entry
      echo 'Not found';
    }
    PHP:
     
    CodedCaffeine, Jan 2, 2010 IP
    Pixel T. likes this.
  15. Pixel T.

    Pixel T. Well-Known Member

    Messages:
    1,205
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    170
    #15
    Ahhh your the man

    Thanks JAY6390 too.

    Rep added :)
     
    Pixel T., Jan 2, 2010 IP