Are there any specific tutorials or resources available for writing a search script in PHP?
To write a search script in PHP, you can utilize SQL queries to retrieve data from a database based on user input. You can create a form where users can input their search query, then use PHP to process the input and construct a SQL query to fetch relevant data from the database.
<?php
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Process user input
$search_query = $_GET['search_query'];
// Construct SQL query
$sql = "SELECT * FROM table_name WHERE column_name LIKE '%$search_query%'";
// Execute SQL query
$result = $conn->query($sql);
// Display search results
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "ID: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
}
} else {
echo "No results found";
}
// Close database connection
$conn->close();
?>
Keywords
Related Questions
- How can beginners in Zend2 ensure they are following MVC principles correctly when developing PHP applications?
- What are the best practices for initializing classes and handling dependencies in PHP, especially when dealing with multiple classes?
- What are the best practices for handling form data in PHP when using the $_POST array?