How can PHP developers handle dynamic text content retrieved from a database when implementing search functionality?
When handling dynamic text content retrieved from a database for search functionality, PHP developers can use SQL queries with LIKE clauses to search for specific keywords within the text. This allows for flexible and efficient searching of text content stored in the database.
// Assuming $keyword contains the search term input by the user
$keyword = $_GET['keyword'];
// Connect to the database
$conn = new mysqli($servername, $username, $password, $dbname);
// SQL query to retrieve text content containing the search term
$sql = "SELECT * FROM text_content_table WHERE text_content LIKE '%$keyword%'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Output data of each row
while($row = $result->fetch_assoc()) {
echo "Text Content: " . $row["text_content"]. "<br>";
}
} else {
echo "No results found";
}
$conn->close();
Related Questions
- How can PHP be integrated with tools like ffmpeg or JavaScript to enhance the functionality of a video generator?
- Are there any best practices for securely filtering out PHP commands from user input in PHP applications?
- How can the meta-refresh tag in PHP be used effectively in form submissions to maintain user experience and avoid white screen errors?