How to search for a specific message with Webklex/PHP?

Updated: Feb 22, 2025

How to search for a specific message with Webklex/PHP?

To search for a specific message using Webklex/PHP, you can follow these steps:

  1. Create a search form: First, you need to create a search form where users can input the keyword or phrase they want to search for. Here's an example of how to create a simple search form using HTML and PHP:
<form action="search.php" method="get">
  <input type="text" name="search_query" placeholder="Search for a message...">
  <button type="submit">Search</button>
</form>
  1. Handle the search query in PHP: In the search.php file, you can handle the search query using PHP. Here's an example of how to retrieve the search query from the URL and store it in a variable:
<?php
$search_query = isset($_GET['search_query']) ? $_GET['search_query'] : '';
?>
  1. Search for the message: To search for a specific message, you need to query the database using the search query. Here's an example of how to search for messages containing the search query using MySQLi:
<?php
require_once 'db_config.php';

$search_query = mysqli_real_escape_string($conn, $search_query);
$query = "SELECT * FROM messages WHERE message LIKE '%$search_query%'";
$result = mysqli_query($conn, $query);

if (mysqli_num_rows($result) > 0) {
  // Display the search results
  while ($row = mysqli_fetch_assoc($result)) {
    echo $row['message'] . ' - ' . $row['sender'] . ' - ' . $row['timestamp'] . '<br>';
  }
} else {
  echo 'No messages found.';
}

mysqli_close($conn);
?>

In this example, we're using the LIKE operator to search for messages containing the search query anywhere in the message text. We're also using MySQLi to execute the query and fetch the results.

  1. Display the search results: Once you've retrieved the search results, you can display them to the user. In this example, we're simply echoing each message along with the sender and timestamp:
while ($row = mysqli_fetch_assoc($result)) {
  echo $row['message'] . ' - ' . $row['sender'] . ' - ' . $row['timestamp'] . '<br>';
}
  1. Sanitize user input: It's important to sanitize user input to prevent SQL injection attacks. In this example, we're using mysqli_real_escape_string() to escape special characters in the search query before executing the query.

That's it! With these steps, you should be able to search for specific messages using Webklex/PHP.