How to list all databases in MySQL with PHP

墨辰丷
Release: 2023-03-31 20:08:02
Original
2074 people have browsed it

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( &#39;NL&#39;, "\n" );
define( &#39;TB&#39;, &#39; &#39; );
// connecting to MySQL.
$conn = @mysql_connect( &#39;localhost&#39;, &#39;username&#39;, &#39;password&#39; )
    or die( mysql_errno().&#39;: &#39;.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 &#39;<ul class="projects">&#39;.NL;
 ///* USING: mysql_fetch_object()
 // ---------------------------
 while( $row = mysql_fetch_object( $result ) ):
  echo TB.&#39;<li>&#39;.$row->Database.&#39;</li>&#39;.NL;
 endwhile;
 //*/
 /* USING: mysql_fetch_row()
 // ------------------------
 while( $row = mysql_fetch_row( $result ) ):
  echo TB.&#39;<li>&#39;.$row[0].&#39;</li>&#39;.NL;
 endwhile;
 //*/
 /* USING: mysql_fetch_assoc()
 // --------------------------
 while( $row = mysql_fetch_assoc( $result ) ):
  echo TB.&#39;<li>&#39;.$row[&#39;Database&#39;].&#39;</li>&#39;.NL;
 endwhile;
 //*/
echo &#39;</ul>&#39;.NL;
// Free resources / close MySQL Connection
mysql_free_result( $result );
mysql_close( $conn );  
?>
Copy after login

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

How to use foreach() in PHP

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!

Related labels:
source:php.cn
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!