PHP如何获取数据库表中个字段的数据
1个回答
推荐于2018-03-23
展开全部
<?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";//sql查询语句
$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();
?>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询