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

php读取mysql入门实例

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

例子,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. // Check connection
  8. if (mysqli_connect_errno()){
  9. echo "Failed to connect to MySQL: " . mysqli_connect_error();
  10. } bbs.it-home.org
  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. ?>

人气教程排行