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

PHP 列出 MySQL 中所有的数据库

程序员文章站 2022-06-09 18:20:23
...
  1. define( 'NL', "\n" );
  2. define( 'TB', ' ' );
  3. // connecting to MySQL.
  4. $conn = @mysql_connect( 'localhost', 'username', 'password' )
  5. or die( mysql_errno().': '.mysql_error().NL );
  6. // attempt to get a list of MySQL databases
  7. // already set up in my account. This is done
  8. // using the PHP function: mysql_list_dbs()
  9. $result = mysql_list_dbs( $conn );
  10. // Output the list
  11. echo '
      '.NL;
    • ///* USING: mysql_fetch_object()
    • // ---------------------------
    • while( $row = mysql_fetch_object( $result ) ):
    • echo TB.'
    • '.$row->Database.'
    • '.NL;
    • endwhile;
    • //*/
    • /* USING: mysql_fetch_row()
    • // ------------------------
    • while( $row = mysql_fetch_row( $result ) ):
    • echo TB.'
    • '.$row[0].'
    • '.NL;
    • endwhile;
    • //*/
    • /* USING: mysql_fetch_assoc()
    • // --------------------------
    • while( $row = mysql_fetch_assoc( $result ) ):
    • echo TB.'
    • '.$row['Database'].'
    • '.NL;
    • endwhile;
    • //*/
    • echo '
    '.NL;
  12. // Free resources / close MySQL Connection
  13. mysql_free_result( $result );
  14. mysql_close( $conn );
  15. ?>
复制代码

PHP, MySQL