当前位置:Gxlcms > PHP教程 > PHP列出MySQL中所有的数据库

PHP列出MySQL中所有的数据库

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

  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

人气教程排行