MySQL的三個資訊:
- 查詢的結果有關的資訊: 這包括由任何SELECT,UPDATE或DELETE語句產生數量的記錄。
- 表和資料庫有關的資訊: 這包括表和資料庫的結構有關的資訊。
- MySQL伺服器的資訊: 這包括目前狀態的資料庫伺服器,版本號碼等。
在mysql提示符下很容易獲得所有這些資訊。但是,當使用Perl或PHP的API,那麼我們就需要顯式地調用各種API獲得所有這些資訊。下面的部分將告訴你如何獲得這些資訊。
擷取查詢所影響的行數:
PERL 執行個體:
在DBI指令碼中,受影響的行數由do()或execute()方法返回,這取決於如何執行查詢:
# Method 1# execute $query using do( )my $count = $dbh->do ($query);# report 0 rows if an error occurredprintf "%d rows were affected\n", (defined ($count) ? $count : 0);# Method 2# execute query using prepare( ) plus execute( )my $sth = $dbh->prepare ($query);my $count = $sth->execute ( );printf "%d rows were affected\n", (defined ($count) ? $count : 0);
PHP 執行個體:
在PHP中,調用mysql_affected_rows()函數找出多少行查詢改變:
$result_id = mysql_query ($query, $conn_id);# report 0 rows if the query failed$count = ($result_id ? mysql_affected_rows ($conn_id) : 0);print ("$count rows were affected\n");
表和資料庫清單(列表):
很容易列出了所有與資料庫伺服器的資料庫和表。如果沒有足夠的許可權結果可能是空的。
除了方法,我剛才提到下面可以使用SHOW TABLES或SHOW DATABASES查詢的表或資料庫的列表,無論是在PHP或PERL。
PERL 執行個體:
# Get all the tables available in current database.my @tables = $dbh->tables ( );foreach $table (@tables ){ print "Table Name $table\n";}
PHP 執行個體:
<?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可以執行下面的命令是mysql提示符下使用,或使用任何指令碼,如PHP,資料庫伺服器擷取各種重要訊息。