php读取redsources_db数据库的red_resources;数据库账号为root,密码为Aa123456。red_resources表头内容为id,category_id,subcategory_id,details,image。 根据数据库动态创建以下内容: 第一个php文件: 需求1:接受外部传入的$subcategoryId = $_GET['subcategory']; 需求2:根据传入的subcategory_id,在数据库中查询该subcategory_id的相同种类的总数量; 需求3:显示subcategory_id相同种类的所有信息; 请给出完整代码
时间: 2024-03-23 20:42:33 浏览: 95
ACCESS数据库密码读取专家.rar_Access 密码读取_access
以下是代码示例:
```php
<?php
// 数据库连接信息
$servername = "localhost";
$username = "root";
$password = "Aa123456";
$dbname = "redsources_db";
// 创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);
// 检查连接
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// 获取传入的 subcategory_id
$subcategoryId = $_GET['subcategory'];
// 查询相同种类的总数量
$sql = "SELECT COUNT(*) as total FROM red_resources WHERE subcategory_id = '$subcategoryId'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$total = $row['total'];
// 显示相同种类的所有信息
$sql = "SELECT * FROM red_resources WHERE subcategory_id = '$subcategoryId'";
$result = mysqli_query($conn, $sql);
echo "总共有 " . $total . " 条记录:<br>";
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - category_id: " . $row["category_id"]. " - subcategory_id: " . $row["subcategory_id"]. " - details: " . $row["details"]. " - image: " . $row["image"]. "<br>";
}
} else {
echo "0 结果";
}
// 关闭连接
mysqli_close($conn);
?>
```
注意:该代码中未对传入的 subcategory_id 进行任何过滤或验证,仅作为示例代码。在实际应用中应该对传入的参数进行安全性检查和过滤。
阅读全文