php两个表建立连接后怎样查询数据库2
发布网友
发布时间:2023-10-10 15:01
我来回答
共1个回答
热心网友
时间:2024-11-19 06:37
<?php
$servername = "localhost";
$username = "root";
$password = "password";//你的mysql密码
$dbname = "myDB";//选择数据库
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// 输出每行数据
while($row = $result->fetch_assoc()) {
echo "<br> id: ". $row["id"]. " - Name: ". $row["firstname"]. " " . $row["lastname"];
}
} else {
echo "0 results";
}
$conn->close();
?>