Home >Backend Development >PHP Tutorial >PHP lists all databases in MySQL

PHP lists all databases in MySQL

WBOY
WBOYOriginal
2016-07-25 08:42:341162browse
  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


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn