欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php读取mysql数据库入门实例

程序员文章站 2024-02-06 17:51:40
...

php读取mysql数据库入门实例

完整代码:

  1. $hostname = "localhost:3306";

  2. $username = "root";
  3. $password = "root";
  4. $dbname = "test";
  5. $conn = mysqli_connect($hostname, $username, $password, $dbname);
  6. $conn->query("SET NAMES 'gbk'");
  7. // 检测数据连接
  8. if (mysqli_connect_errno()){
  9. echo "Failed to connect to MySQL: " . mysqli_connect_error();
  10. }
  11. $sql = "select * from keyword";
  12. $result = mysqli_query($conn, $sql);
  13. //输出mysql数据查询结果

  14. while($row = mysqli_fetch_array($result)){
  15. echo $row['keycId'] . " : " .$row['keycontent'] . " : " . $row['keycause'];
  16. echo "
    ";
  17. }
  18. // 关闭连接
  19. mysqli_close($conn);
  20. ?>
复制代码