html文本如何读取数据库代码示例

2024年11月04日 建站教程

<!DOCTYPE html>  
<html>  
<head>  
  <title>读取数据库示例</title>  
</head>  
<body>  
  <h1>用户列表</h1>  
  <ul>  
    <?php  
	 // 数据库连接信息  
	 $servername = "localhost";  
	 $username = "username";  
	 $password = "password";  
	 $dbname = "myDB";  
	    
	 // 创建连接  
	 $conn = new mysqli($servername, $username, $password, $dbname);  
	    
	 // 检测连接  
	 if ($conn->connect_error) {  
	 	die("连接失败: " . $conn->connect_error);  
	 }  
	    
	 // 执行SQL查询  
	 $sql = "SELECT id, name FROM users";  
	 $result = $conn->query($sql);  
	    
	 if ($result->num_rows > 0) {  
	 	// 输出数据  
	 	while($row = $result->fetch_assoc()) {  
	 	  echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>";  
	 	}  
	 } else {  
	 	echo "0 结果";  
	 }  
	 $conn->close();  
	?>
  </ul>  
</body>  
</html>

本文链接:http://so.lmcjl.com/news/16957/

展开阅读全文