PHP本地离线电影网站搜索异常及数据库连接错误求助
Hey there, let's work through this step by step to get your offline PHP movie site up and running—first fixing that database connection error, then getting the search functionality working.
That "target computer actively declined the connection" error almost always boils down to one of a few simple issues:
Check if MySQL/MariaDB is running in XAMPP
Open your XAMPP control panel and look at the MySQL module. If it's not marked as Running, click the Start button to fire it up. Sometimes the service fails to start automatically, or gets blocked by antivirus/firewall software—if it won't start, check the XAMPP log for clues, or temporarily disable your antivirus to test (don't forget to re-enable it afterward).Double-check your connection parameters
Open yourzoeken.phpfile and find themysqli_connect()line. Make sure your credentials match XAMPP's default setup (unless you've modified them):- Host: Use
127.0.0.1instead oflocalhostsometimes fixes IPv6-related connection issues - Username: Default is
root - Password: Default is blank (leave it empty if you never set one)
- Database name: Ensure this matches the exact name of your movie database (no typos!)
Here's a corrected connection snippet to use:
$conn = mysqli_connect('127.0.0.1', 'root', '', 'your_movie_db_name'); if (!$conn) { die("Connection failed: " . mysqli_connect_error()); }- Host: Use
Verify the MySQL port (if you changed it)
If you modified XAMPP's default MySQL port (usually 3306), you need to add it as the 5th parameter inmysqli_connect():$conn = mysqli_connect('127.0.0.1', 'root', '', 'your_movie_db_name', 3307); // Example for port 3307
Once your database connects properly, here's a secure, functional implementation for searching by title, year, genre, and director—with protection against SQL injection:
Step 1: The Search Form
First, add this HTML form (you can drop it in zoeken.php or a separate file that submits to it):
<form method="get" action="zoeken.php"> <input type="text" name="title" placeholder="Search by title..."> <input type="number" name="year" placeholder="Search by year (e.g., 2020)"> <input type="text" name="genre" placeholder="Search by genre..."> <input type="text" name="director" placeholder="Search by director..."> <button type="submit">Find Movies</button> </form>
Step 2: The PHP Search Logic
Add this code after your database connection in zoeken.php to handle the search:
// Get user input (use null coalescing to avoid undefined index errors) $title = $_GET['title'] ?? ''; $year = $_GET['year'] ?? ''; $genre = $_GET['genre'] ?? ''; $director = $_GET['director'] ?? ''; // Build a dynamic, safe SQL query $sql = "SELECT * FROM movies WHERE 1=1"; // 1=1 lets us easily add AND conditions $params = []; $paramTypes = ''; // Add title search condition (partial match) if (!empty(trim($title))) { $sql .= " AND title LIKE ?"; $params[] = "%{$title}%"; $paramTypes .= 's'; // 's' = string type } // Add year search condition (exact match) if (!empty(trim($year))) { $sql .= " AND year = ?"; $params[] = $year; $paramTypes .= 'i'; // 'i' = integer type } // Add genre search condition (partial match) if (!empty(trim($genre))) { $sql .= " AND genre LIKE ?"; $params[] = "%{$genre}%"; $paramTypes .= 's'; } // Add director search condition (partial match) if (!empty(trim($director))) { $sql .= " AND director LIKE ?"; $params[] = "%{$director}%"; $paramTypes .= 's'; } // Execute the query safely with prepared statements $stmt = mysqli_prepare($conn, $sql); if ($stmt) { // Bind parameters if we have any if (!empty($params)) { mysqli_stmt_bind_param($stmt, $paramTypes, ...$params); } mysqli_stmt_execute($stmt); $result = mysqli_stmt_get_result($stmt); // Display results if (mysqli_num_rows($result) > 0) { while ($movie = mysqli_fetch_assoc($result)) { echo "<p>"; echo "<strong>Title:</strong> " . htmlspecialchars($movie['title']) . "<br>"; echo "<strong>Year:</strong> " . htmlspecialchars($movie['year']) . "<br>"; echo "<strong>Genre:</strong> " . htmlspecialchars($movie['genre']) . "<br>"; echo "<strong>Director:</strong> " . htmlspecialchars($movie['director']); echo "</p>"; } } else { echo "<p>No movies found matching your search criteria.</p>"; } // Clean up mysqli_stmt_close($stmt); } else { echo "<p>Error preparing search query: " . mysqli_error($conn) . "</p>"; } // Close database connection mysqli_close($conn);
This code handles partial matches for text fields (title, genre, director) and exact matches for the year, while using prepared statements to keep your database safe from SQL injection attacks.
内容的提问来源于stack exchange,提问作者Floris

