在PHP中,我们可以使用PDO(PHP Data Objects)库来操作数据库,包括执行搜索和聚合操作。以下是一些示例:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>
<?php
$searchTerm = "example";
$sql = "SELECT * FROM myTable WHERE column_name LIKE :searchTerm";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':searchTerm', $searchTerm, PDO::PARAM_STR);
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<?php
$sql = "SELECT COUNT(*) as total FROM myTable";
$stmt = $conn->prepare($sql);
$stmt->execute();
$totalResult = $stmt->fetch(PDO::FETCH_ASSOC);
$total = $totalResult['total'];
?>
<?php
$sql = "SELECT AVG(column_name) as average FROM myTable";
$stmt = $conn->prepare($sql);
$stmt->execute();
$averageResult = $stmt->fetch(PDO::FETCH_ASSOC);
$average = $averageResult['average'];
?>
<?php
$sql = "SELECT MIN(column_name) as min FROM myTable";
$stmt = $conn->prepare($sql);
$stmt->execute();
$minResult = $stmt->fetch(PDO::FETCH_ASSOC);
$min = $minResult['min'];
?>
<?php
$sql = "SELECT MAX(column_name) as max FROM myTable";
$stmt = $conn->prepare($sql);
$stmt->execute();
$maxResult = $stmt->fetch(PDO::FETCH_ASSOC);
$max = $maxResult['max'];
?>
这些示例展示了如何使用PHP和PDO库连接到数据库、执行搜索和聚合操作。请根据您的具体需求修改数据库连接信息、表名和列名。