quering a database using mysql/php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sarah333
    New Member
    • Aug 2014
    • 1

    quering a database using mysql/php

    i want to display data in a table based on the user selection from a form (on a previous page).

    my query isnt working:

    $sth = $conn->prepare ('SELECT * FROM ROUTE WHERE ORIGIN = $ORIG');

    the $ORIG is the value of the options inside the drop down list. (its the value of one option in the form on the previous page)
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    it’s not working because prepared statements don’t work like that.

    example usage (PDO):
    Code:
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare('SELECT * FROM ROUTE WHERE ORIGIN = ?');
    // assuming string type parameter
    $stmt->bindValue(1, $ORIG, PDO::PARAM_STR);
    $stmt->execute();
    foreach ($stmt as $row) {
        // do something with $row
    }

    Comment

    Working...