PHP5i如何连接MySQL数据库和读取数据

来源:互联网 发布:php彩票开奖源码 编辑:程序博客网 时间:2024/06/06 00:17
  1. <?php     
  2.         
  3.     /* Connect to a MySQL server  连接数据库服务器 */     
  4.     $link = mysqli_connect(     
  5.                 'localhost',  /* The host to connect to 连接MySQL地址 */     
  6.                 'user',      /* The user to connect as 连接MySQL用户名 */     
  7.                 'password',  /* The password to use 连接MySQL密码 */     
  8.                 'world');    /* The default database to query 连接数据库名称*/     
  9.         
  10.     if (!$link) {     
  11.        printf("Can't connect to MySQL Server. Errorcode: %s ", mysqli_connect_error());     
  12.        exit;     
  13.     }     
  14.         
  15.     /* Send a query to the server 向服务器发送查询请求*/     
  16.     if ($result = mysqli_query($link, 'SELECT Name, Population FROM City ORDER BY Population DESC LIMIT 5')) {     
  17.         
  18.         //print("Very large cities are: ");     
  19.         
  20.         /* Fetch the results of the query 返回查询的结果 */     
  21.         while( $row = mysqli_fetch_assoc($result) ){     
  22.             printf("%s (%s) ", $row['Name'], $row['Population']);     
  23.         }     
  24.         
  25.         /* Destroy the result set and free the memory used for it 结束查询释放内存 */     
  26.         mysqli_free_result($result);     
  27.     }     
  28.         
  29.     /* Close the connection 关闭连接*/     
  30.     mysqli_close($link);     
  31.     ?> 
原创粉丝点击