你可能想知道MySQL以下三種信息:
在MySQL的命令提示符中,我們可以很容易的獲取以上服務(wù)器信息。 但如果使用Perl或PHP等腳本語言,你就需要調(diào)用特定的接口函數(shù)來獲取。 接下來我們會詳細介紹。
在 DBI 腳本中, 語句影響的記錄數(shù)通過函數(shù) do( ) 或 execute( )返回:
# 方法 1 # 使用do( ) 執(zhí)行 $query my $count = $dbh->do ($query); # 如果發(fā)生錯誤會輸出 0 printf "%d rows were affected\n", (defined ($count) ? $count : 0); # 方法 2 # 使用prepare( ) 及 execute( ) 執(zhí)行 $query my $sth = $dbh->prepare ($query); my $count = $sth->execute ( ); printf "%d rows were affected\n", (defined ($count) ? $count : 0);
在PHP中,你可以使用 mysql_affected_rows( ) 函數(shù)來獲取查詢語句影響的記錄數(shù)。
$result_id = mysql_query ($query, $conn_id); # 如果查詢失敗返回 $count = ($result_id ? mysql_affected_rows ($conn_id) : 0); print ("$count rows were affected\n");
你可以很容易的在MySQL服務(wù)器中獲取數(shù)據(jù)庫和數(shù)據(jù)表列表。 如果你沒有足夠的權(quán)限,結(jié)果將返回 null。
你也可以使用 SHOW TABLES 或 SHOW DATABASES 語句來獲取數(shù)據(jù)庫和數(shù)據(jù)表列表。
# 獲取當(dāng)前數(shù)據(jù)庫中所有可用的表。 my @tables = $dbh->tables ( ); foreach $table (@tables ){ print "Table Name $table\n"; }
<?php $con = mysql_connect("localhost", "userid", "password"); if (!$con) { die('Could not connect: ' . mysql_error()); } $db_list = mysql_list_dbs($con); while ($db = mysql_fetch_object($db_list)) { echo $db->Database . "<br />"; } mysql_close($con); ?>
以下命令語句可以在MySQL的命令提示符使用,也可以在腳本中 使用,如PHP腳本。
命令 | 描述 |
---|---|
SELECT VERSION( ) | 服務(wù)器版本信息 |
SELECT DATABASE( ) | 當(dāng)前數(shù)據(jù)庫名 (或者返回空) |
SELECT USER( ) | 當(dāng)前用戶名 |
SHOW STATUS | 服務(wù)器狀態(tài) |
SHOW VARIABLES | 服務(wù)器配置變量 |