當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


PHP mysqli_stat()用法及代碼示例


定義和用法

這個mysqli_stat()函數檢索並返回當前服務器的信息/狀態。此信息包括有關服務器的詳細信息,例如線程數、打開的表數、正常運行時間等。

用法

mysqli_stat($con)

參數

Sr.No 參數及說明
1

con(Mandatory)

這是一個表示與 MySQL 服務器的連接的對象。

返回值

PHP mysqli_stat() 函數返回一個字符串值,表示當前 MySQL 服務器的狀態。如果發生錯誤,此函數將返回布爾值 false。

PHP版本

這個函數最初是在 PHP 版本 5 中引入的,適用於所有後續版本。

示例

以下示例演示了 mysqli_stat() 函數的用法(程序風格) -

<?php
   //Creating a connection
   $con = mysqli_connect("localhost", "root", "password", "mydb");

   //Status
   $stat = mysqli_stat($con);
   print("Status:".$stat);

   //Closing the connection
   mysqli_close($con);
?>

這將產生以下結果 -

Status:Uptime:130131  Threads:2  Questions:350  Slow queries:0  Opens:172  Flush tables:1  Open tables:145  Queries per second avg:0.002

示例

在麵向對象風格中,這個函數的語法是 $con->stat();。以下是麵向對象風格的此函數的示例 -

<?php
   //Creating a connection
   $con = new mysqli("localhost", "root", "password", "mydb");

   //Status
   $stat = $con->stat();
   print("Status:".$stat);

   //Closing the connection
   $con -> close();
?>

這將產生以下結果 -

Status:Uptime:131057  Threads:2  Questions:354  Slow queries:0  Opens:172  Flush tables:1  Open tables:145  Queries per second avg:0.002

示例

<?php
   $connection_mysql = mysqli_connect("localhost", "root", "password", "mydb");
   
   if (mysqli_connect_errno($connection_mysql)){
      echo "Failed to connect to MySQL:" . mysqli_connect_error();
   }
   
   echo "System status:". mysqli_stat($connection_mysql); 
   
   mysqli_close($connection_mysql);
?>

這將產生以下結果 -

System status:Uptime:131468  Threads:2  Questions:356  Slow queries:0  Opens:172  Flush tables:1  Open tables:145  Queries per second avg:0.002

相關用法


注:本文由純淨天空篩選整理自 PHP mysqli_stat() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。