HELP PHP problem

Discussion in 'PHP' started by shadow007, May 13, 2010.

  1. #1
    Hi

    I have created a form which passes a variable and I am trying to use this stored variable to dictate the output of my sql command.

    Basically I have tried the following:

    SELECT *
    FROM EXAMPLE
    WHERE Example.Seat=$input_value

    What my intentions are is to enter a value e.g 3 and then the 3 rows in the database are shown.

    Can anyone help me with this Im really stuck and appreciate any help

    Thanks
     
    shadow007, May 13, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    Add LIMIT 3 to your query
     
    danx10, May 13, 2010 IP
  3. swashata

    swashata Member

    Messages:
    86
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #3
    Say you are using GET method to pass the variable through a form and it is row_no. Here is the PHP code you need

    $limit = $_GET['row_no'];
    $dbc = mysqli_connect($dbHost, $dbUser, $dbPass, $dbName);
    $query = "SELECT * FROM EXAMPLE WHERE table_field='table_value' LIMIT $limit";
    $result = mysqli_query($dbc, $query);
    //Other MySQL stuffs, like fetching rows etc
    mysqli_close($dbc);
    PHP:
    If you have any problem, feel free to ask!
     
    swashata, May 13, 2010 IP
  4. phpSiteMinder

    phpSiteMinder Peon

    Messages:
    47
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You should filter all user input. Not doing so is just asking for trouble.
     
    phpSiteMinder, May 15, 2010 IP
  5. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #5
    Yep! That's right!
    You need to filter that :)
     
    roopajyothi, May 15, 2010 IP
  6. swashata

    swashata Member

    Messages:
    86
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #6
    Oh yaa!
    $limit = (int) $_GET['row_no'];
    $dbc = mysqli_connect($dbHost, $dbUser, $dbPass, $dbName);
    $query = "SELECT * FROM EXAMPLE WHERE table_field='table_value' LIMIT $limit";
    $result = mysqli_query($dbc, $query);
    //Other MySQL stuffs, like fetching rows etc
    mysqli_close($dbc);
    PHP:
    Typecasting will work here best I guess! No need to mysqli_real_escape_string and other escaping! :)
     
    swashata, May 15, 2010 IP
  7. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #7
    Or intval()
     
    danx10, May 16, 2010 IP
  8. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #8
    Yea intval() can be used it returns the integer value of variable, using the specified base for the conversion (the default is base 10).
     
    roopajyothi, May 16, 2010 IP