I am creating a search to search by name. In the database I have first name (fname
) and surname (lname
).
The search works when you search by either first name or last name but not the full name.
e.g Entering 'Joe' or 'Smith' as the value will bring back 'Joe Smith'. Entering 'Joe Smith' as the value will bring back no records.
$value = "%".$search_val."%";
$query = "SELECT fname, lname FROM users WHERE (fname LIKE ? OR lname LIKE ?)";
if ($stmt = $conn->prepare($query)) {
$stmt->bind_param('ss', $value, $value);
$stmt->execute();
$stmt->bind_result($fname, $lname);
/* fetch values */
while ($stmt->fetch()) {
printf ("%s %s\n <br>", $fname, $lname);
}
/* close statement */
$stmt->close();
}
/* close connection */
$conn->close();
?>
Is there a way to concatenate fname
and lname
within the query? Or is there another solution?