PHP lists all databases in MySQL server

WBOY
Release: 2024-03-22 08:12:02
forward
557 people have browsed it

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);
Copy after login
  1. Execute SHOW DATABASES Query:
$result = $conn->query("SHOW DATABASES");
Copy after login
  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";
}
Copy after login

Full code example:

Copy after login

Other methods:

Use MySQL command line tools:

mysql -u root -p
SHOW DATABASES;
Copy after login

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!

source:lsjlt.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!