Fehler beim prepared statement mit ?

  • PHP

Es gibt 4 Antworten in diesem Thema. Der letzte Beitrag () ist von slice.

    Fehler beim prepared statement mit ?

    nabend, es geht um den nachfolgenden code, weiß jemand wie ich es funktionsfähig machen kann?

    PHP-Quellcode

    1. // Prepare a statement to retrieve table names containing 'sammlungen'
    2. $stmt = $conn->prepare("SHOW TABLES LIKE ?");
    3. // Define the parameter with a wildcard pattern
    4. $pattern = '%sammlungen%';
    5. // Bind the parameter to the statement
    6. $stmt->bind_param("s", $pattern);
    7. // Execute the statement
    8. $stmt->execute();
    9. // Store the results in an array
    10. $result = $stmt->get_result();
    11. $tables = array();


    gibt folgenden Fehler aus:
    Fatal error: Uncaught mysqli_sql_exception: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '?'

    mit folgendem code funktioniert es, jedoch ist da kein prepared statement drinne:

    PHP-Quellcode

    1. $result = $conn->query("SHOW TABLES LIKE '%sammlungen%'");
    2. $tables = array();


    LG

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Visual_Prog“ ()

    Ich tippe mal auf die Single Quote, aber wirf auch hier ein Blick drauf: PDOStatement::bindParam
    Der top Kommentar dürfte dir helfen:
    I know this has been said before but I'll write a note on it too because I think it's important to keep in mind:

    If you use PDO bindParam to do a search with a LIKE condition you cannot put the percentages and quotes to the param placeholder '%:keyword%'.

    This is WRONG:
    "SELECT * FROM `users` WHERE `firstname` LIKE '%:keyword%'";

    The CORRECT solution is to leave clean the placeholder like this:
    "SELECT * FROM `users` WHERE `firstname` LIKE :keyword";

    And then add the percentages to the php variable where you store the keyword:
    $keyword = "%".$keyword."%";

    And finally the quotes will be automatically added by PDO when executing the query so you don't have to worry about them.

    So the full example would be:

    PHP-Quellcode

    1. <?php
    2. // Get the keyword from query string
    3. $keyword = $_GET['keyword'];
    4. // Prepare the command
    5. $sth = $dbh->prepare('SELECT * FROM `users` WHERE `firstname` LIKE :keyword');
    6. // Put the percentage sing on the keyword
    7. $keyword = "%".$keyword."%";
    8. // Bind the parameter
    9. $sth->bindParam(':keyword', $keyword, PDO::PARAM_STR);
    10. ?>
    hey danke schon mal für die antwort.
    Leider will das noch nicht so recht klappen. Habe schon verschiedene Mal umformuliert aber der Fehler bleibt.

    Hat es evtl. damit zu tun, dass meine Variable nicht über GET übertragen wrid?

    ich tappe da echt ein wenig im dunkeln :/

    EDIT : ich glaub ich habs ^^

    PHP-Quellcode

    1. ​$stmt = $conn->prepare("SELECT table_name FROM information_schema.tables WHERE table_schema = 'mydatabase' AND table_name LIKE ?");

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Visual_Prog“ ()

    board.phpbuilder.com/d/1036642…tables-with-prepared-stmt

    To sum up what we've found out: SHOW TABLES cannot be used with parameters in a prepared statement.

    Daher geht es nur mit der von dir zuletzt genannten Anweisung.
    Besucht auch mein anderes Forum:
    Das Amateurfilm-Forum