PHP 和 MySQL - 資料庫資訊示例



MySQL 中有一些重要命令,可以在 MySQL 提示符下執行,或者使用任何指令碼(如 PHP)來獲取有關資料庫伺服器的各種重要資訊。

序號 命令和描述
1

SELECT VERSION( )

伺服器版本字串

2

SELECT DATABASE( )

當前資料庫名稱(無則為空)

3

SELECT USER( )

當前使用者名稱

4

SHOW STATUS

伺服器狀態指示器

5

SHOW VARIABLES

伺服器配置變數

示例

嘗試以下示例以獲取資料庫資訊 -

將以下示例複製並貼上為 mysql_example.php -

<html>
   <head>
      <title>Getting MySQL Database Info</title>
   </head>
   <body>
      <?php
         $dbhost = 'localhost';
         $dbuser = 'root';
         $dbpass = 'root@123';
         $dbname = 'TUTORIALS';
         $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
         $tutorial_count = null;
		 
         if($mysqli->connect_errno ) {
            printf("Connect failed: %s<br />", $mysqli->connect_error);
            exit();
         }
         printf('Connected successfully.<br />');
		 
         if ($result = mysqli_query($mysqli, "SELECT DATABASE()")) {
            $row = mysqli_fetch_row($result);
            printf("Default database is %s<br />", $row[0]);
            mysqli_free_result($result);
         }
         $mysqli->close();
      ?>
   </body>
</html>

輸出

訪問部署在 Apache Web 伺服器上的 mysql_example.php 並驗證輸出。

Connected successfully.
Default database is tutorials
廣告