How can PHP be used to search for multiple parameters in a database?
When searching for multiple parameters in a database using PHP, you can construct a SQL query with the WHERE clause to include all the parameters you want to search for. You can use the AND or OR operators to combine multiple conditions in the WHERE clause to search for records that meet all the specified criteria.
<?php
// Database connection
$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);
}
// Search parameters
$param1 = "value1";
$param2 = "value2";
// SQL query with multiple parameters
$sql = "SELECT * FROM table_name WHERE column1 = '$param1' AND column2 = '$param2'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Output data of each row
while($row = $result->fetch_assoc()) {
echo "Column1: " . $row["column1"]. " - Column2: " . $row["column2"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Keywords
Related Questions
- What best practices should be followed when moving or renaming temporary files in PHP after an upload?
- What are some best practices for beginners in PHP when dealing with filtering and manipulating arrays containing numerical values?
- What are some potential pitfalls of using mysql_fetch_array in PHP and how can they be avoided?