<?php
$mysql_stat=mysql_stat($dbconnect);
$tot_count=preg_match_all('/([a-z ]+):\s*([0-9.]+)/i', $mysql_stat, $matches);
for ($i=0; $i<$tot_count; $i++) {
$info1=$matches[1][$i];
$info2=$matches[2][$i];
echo "$info1 ($info2)<br>";
}
/*
example output:
Uptime (13651)
Threads (1)
Questions (12910)
Slow queries (0)
Opens (314)
Flush tables (1)
Open tables (24)
Queries per second avg (0.945)
*/
?>
mysql_stat
(PHP 4 >= 4.3.0, PHP 5)
mysql_stat — 現在のシステムの状態を取得する
説明
string mysql_stat
([ resource $link_identifier
] )
mysql_stat() は現在のサーバの状態を返します。
パラメータ
- link_identifier
-
MySQL 接続。 指定されない場合、mysql_connect() により直近にオープンされたリンクが 指定されたと仮定されます。そのようなリンクがない場合、引数を指定せずに mysql_connect() がコールした時と同様にリンクを確立します。 リンクが見付からない、または、確立できない場合、 E_WARNING レベルのエラーが生成されます。
返り値
稼働時間、スレッド、クエリ、オープンされているテーブル、フラッシュされた テーブル、そして 1 秒あたりのクエリ数を文字列で返します。その他のデータも 含めた完全な状態を得るには、SHOW STATUS SQL コマンドを 実行する必要があります。link_identifier が不正な 場合には NULL が返されます。
例
例1 mysql_stat() の例
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
$status = explode(' ', mysql_stat($link));
print_r($status);
?>
上の例の出力は、 たとえば以下のようになります。
Array
(
[0] => Uptime: 5380
[1] => Threads: 2
[2] => Questions: 1321299
[3] => Slow queries: 0
[4] => Opens: 26
[5] => Flush tables: 1
[6] => Open tables: 17
[7] => Queries per second avg: 245.595
)
例2 mysql_stat() の別の例
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
$result = mysql_query('SHOW VARIABLES', $link);
while ($row = mysql_fetch_assoc($result)) {
echo $row['Variable_name'] . ' = ' . $row['Value'] . "\n";
}
?>
上の例の出力は、 たとえば以下のようになります。
back_log = 50 basedir = /usr/local/ bdb_cache_size = 8388600 bdb_log_buffer_size = 32768 bdb_home = /var/db/mysql/ bdb_max_lock = 10000 bdb_logdir = bdb_shared_data = OFF bdb_tmpdir = /var/tmp/ ...
mysql_stat
Topper_
02-Sep-2008 08:51
02-Sep-2008 08:51
Enzo_01 at abv dot bg
31-May-2008 07:22
31-May-2008 07:22
<?php
// function that return stat elements into array
function mysql_stat_array($A=null) {
$status = explode(' ', mysql_stat());
foreach($status as $k=>$v) { $v=explode(':',$v,2); $status[$v[0]]=$v[1]; }
return (isset($status[$A])?$status[$A]:$status);
}
/*
example return:
Array ( [0] => Uptime: 385815 [1] => Threads: 1 [2] => Questions: 3515 [3] => Slow queries: 0 [4] => Opens: 32 [5] => Flush tables: 1 [6] => Open tables: 8 [7] => Queries per second avg: 0.009 [Uptime] => Array ( [0] => Uptime [1] => 385815 ) [Threads] => Array ( [0] => Threads [1] => 1 ) [Questions] => Array ( [0] => Questions [1] => 3515 ) [Slow queries] => Array ( [0] => Slow queries [1] => 0 ) [Opens] => Array ( [0] => Opens [1] => 32 ) [Flush tables] => Array ( [0] => Flush tables [1] => 1 ) [Open tables] => Array ( [0] => Open tables [1] => 8 ) [Queries per second avg] => Array ( [0] => Queries per second avg [1] => 0.009 ) )
*/
?>
09-Feb-2005 02:16
if you need the complete status as outputted by the "show status" command. Try this function.
<?
function mysql_status($db=NULL) {
if(!mysql_ping($db))
$db=mysql_connect("localhost","user","password");
$res=mysql_query("show status",$db);
while (list($key,$value)=mysql_fetch_array($res))
$sql[$key]=$value;
return $sql;
}
?>
