Søge-kode: Vis ordet "lommer", selvom der kun søges "lom"
Jeg er ikke helt klar over, hvor der skal rettes i denne kode. Lige nu søger den kun på præcist stavede ord. Jeg kunne godt tænke mig, at den kunne søge på starten af ordet også. Jeg har f.eks. ordet "lommer" i min database. Hvis jeg søger "lomme", kommer der intet resultat.Her er koden:
---------------------------------------
<?php
if (isset($_POST['submit'])) {
//You NEED to use prepared statements when connecting to the database, to make the search form secure! I didn't do that in the video, however we will do it here together!
//If you are confused about prepared statements, please watch episode 40 in the PHP playlist and learn it first.
//Here we get the search input
$search = $_POST['search'];
//ALSO remember that we are using prepared statements, meaning that for now, we will use "?" as a placeholder for $search!
$sql = "SELECT * FROM ordliste WHERE ord_dansk LIKE ?
OR ord_engelsk LIKE ?
OR ord_svensk LIKE ?
OR ord_tysk LIKE ?
";
//Here we start the prepared statement
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
echo "SQL error";
} else {
//Here we assign our data to the prepared statement, and replace our placeholders with $search
mysqli_stmt_bind_param($stmt, 'ssss', $search, $search, $search, $search);
//Then we execute the statement!
mysqli_stmt_execute($stmt);
//Here we get the number of results we got from the search!
$result = mysqli_stmt_get_result($stmt);
$resultCount = mysqli_num_rows($result);
if ($resultCount > 0) {
while ($row = mysqli_fetch_assoc($result)) {
//Here we output the search results!
echo "<tr>";
echo "<td>".$row['ord_dansk']."</td>";
echo "<td>".$row['ord_engelsk']."</td>";
echo "</tr>";
}
}
}
}
?>