Home  >  Article  >  Backend Development  >  PHP lists all databases in MySQL server

PHP lists all databases in MySQL server

WBOY
WBOYforward
2024-03-22 08:10:06536browse

php editor Apple today introduces how to use PHP code to list all databases in the MySQL server. MySQL is a popular relational database management system. The database in the MySQL server can be easily managed through PHP code. In this tutorial, we will demonstrate how to connect to a MySQL server and get a list of all databases, allowing you to quickly understand the database structure in the server. Let’s learn together!

List all databases in MySQL server

Using PHP

step:

  1. Establish a connection to the mysql server:
$servername = "localhost";
$username = "root";
$passWord = "password";

$conn = new mysqli($servername, $username, $password);
  1. Execute SHOW DATABASES Query:
$result = $conn->query("SHOW DATABASES");
  1. Traverse the results and print Database Name:
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["Database"] . "<br>";
}
} else {
echo "No databases found";
}

Full code example:

Other methods:

Use MySQL command line tools:

mysql -u root -p
SHOW DATABASES;

Use phpMyAdmin tool:

  1. Log in to phpMyAdmin.
  2. In the left menu, select "Databases".
  3. All databases in the server will be listed in the right pane.

The above is the detailed content of PHP lists all databases in MySQL server. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lsjlt.com. If there is any infringement, please contact admin@php.cn delete