当前位置:Gxlcms > PHP教程 > php读取mysql数据库入门实例

php读取mysql数据库入门实例

时间:2021-07-01 10:21:17 帮助过:4人阅读

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. ?>

人气教程排行