本地mysql数据库db_test里已建有表friend,数据库的连接用户为root,端口为默认端口,密码为空。friend表字段为:id,name,age,gender,phone,email。请使用PHP连接mysql,查询friend表里age>20的所有字段,输出查询结果以及结果的记录总数。
时间: 2024-03-12 13:47:58 浏览: 81
好的,这里是PHP连接mysql,查询friend表里age>20的所有字段的代码:
```php
<?php
// 连接数据库
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db_test";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// 查询数据
$sql = "SELECT * FROM friend WHERE age > 20";
$result = mysqli_query($conn, $sql);
// 输出查询结果
if (mysqli_num_rows($result) > 0) {
echo "<table border='1'>";
echo "<tr><th>ID</th><th>Name</th><th>Age</th><th>Gender</th><th>Phone</th><th>Email</th></tr>";
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row["id"] . "</td>";
echo "<td>" . $row["name"] . "</td>";
echo "<td>" . $row["age"] . "</td>";
echo "<td>" . $row["gender"] . "</td>";
echo "<td>" . $row["phone"] . "</td>";
echo "<td>" . $row["email"] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "Total records: " . mysqli_num_rows($result);
} else {
echo "0 results";
}
// 关闭连接
mysqli_close($conn);
?>
```
运行这段代码,就可以输出查询结果以及结果的记录总数了。
阅读全文