1

I'm very basic at coding and have the following snippet of code

$connection = new PDO($dsn, $username, $password, $options);

    $sql = "SELECT * 
                    FROM birds
                    WHERE Species Like :Species";
                

    $Species = $_POST['Species'];

    $statement = $connection->prepare($sql);
    $statement->bindParam(':Species', $Species, PDO::PARAM_STR);
    $statement->execute();

    $result = $statement->fetchAll();
} catch(PDOException $error) {
    echo $sql . "<br>" . $error->getMessage();
}

}

it works fine so long as the spelling is correct. how can I add a wildcard to the search feature as all variations I've tried don't seem to be working.

I was also wondering how I could get it to find data in a few columns based on the results entered in form. i.e. search name or location or age from one text field.

lukelloyd
  • 13
  • 4

1 Answers1

1

If you are trying to get partial match, eg if you search "humming" and want it to match "colorful hummingbird", then per this answer, try changing the query to:

SELECT * FROM birds WHERE Species Like CONCAT('%', :Species, '%')

That will still only match where the :Species string itself exists somewhere in the Species column value. It'll be a bit more complicated if you want to be able to actually put wildcard characters in your own search term.

404 Not Found
  • 3,635
  • 2
  • 28
  • 34