Home > Article > Backend Development > How to list all databases in MySQL with PHP
This article mainly introduces the method of PHP to list all databases in MySQL. It involves the skills of operating databases in PHP. It is of great practical value. Friends in need can refer to it.
The example of this article tells the PHP list Methods for all databases in MySQL. The details are as follows:
The PHP code is as follows:
<?php define( 'NL', "\n" ); define( 'TB', ' ' ); // connecting to MySQL. $conn = @mysql_connect( 'localhost', 'username', 'password' ) or die( mysql_errno().': '.mysql_error().NL ); // attempt to get a list of MySQL databases // already set up in my account. This is done // using the PHP function: mysql_list_dbs() $result = mysql_list_dbs( $conn ); // Output the list echo '<ul class="projects">'.NL; ///* USING: mysql_fetch_object() // --------------------------- while( $row = mysql_fetch_object( $result ) ): echo TB.'<li>'.$row->Database.'</li>'.NL; endwhile; //*/ /* USING: mysql_fetch_row() // ------------------------ while( $row = mysql_fetch_row( $result ) ): echo TB.'<li>'.$row[0].'</li>'.NL; endwhile; //*/ /* USING: mysql_fetch_assoc() // -------------------------- while( $row = mysql_fetch_assoc( $result ) ): echo TB.'<li>'.$row['Database'].'</li>'.NL; endwhile; //*/ echo '</ul>'.NL; // Free resources / close MySQL Connection mysql_free_result( $result ); mysql_close( $conn ); ?>
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
How to submit large-scale data in PHP
php method to implement UDP communication based on socket
The above is the detailed content of How to list all databases in MySQL with PHP. For more information, please follow other related articles on the PHP Chinese website!