SQL search query in PHP not working

Viewed 91

Can someone explain why this isnt working? I think this part is wrong, but I don't know how to fix it.

$stmt = $pdo->prepare("SELECT * FROM locatie WHERE gebouw LIKE '%?%' 
AND ruimte LIKE '%?%'");
    $stmt->execute(array($gebouw_zoeken, $ruimte_zoeken));

The problem is it isnt showing the result of the query. There arent any error codes either.

    <table>
        <form action="index.php" method="post">

            Locatie zoeken:<br>
            <input placeholder="Gebouwnaam" type="text" name="gebouw_zoeken" 
>
            <br>
            <input placeholder="Ruimtenaam" type="text" name="ruimte_zoeken" 
>
            <br><br>
            <input  type="submit" name="submit_zoeken" value="Locatie 
zoeken" ><br>
        </form>
    </table><br><br>


    <?php
    if (isset($_POST["submit_zoeken"])) {
        $gebouw_zoeken = $_POST["gebouw_zoeken"];
        $ruimte_zoeken = $_POST["ruimte_zoeken"];
        $stmt = $pdo->prepare("SELECT * FROM locatie WHERE gebouw LIKE '%?%' 
AND ruimte LIKE '%?%'");
        $stmt->execute(array($gebouw_zoeken, $ruimte_zoeken));
        ?>
2 Answers

You need to remove the quotes and put the %'s around the strings you are binding in the execute() statement:

$stmt = $pdo->prepare("SELECT * FROM locatie WHERE gebouw LIKE ? 
AND ruimte LIKE ?");
$stmt->execute(array('%' . $gebouw_zoeken . '%', '%' . $ruimte_zoeken . '%'));

Have you actually declared a variable to hold the array information after executing it?

$data = $stmt->fetchAll();
print_r($data);
Related