調用MySQL中資料庫中繼資料的方法_Mysql

來源:互聯網
上載者:User

 MySQL的三個資訊:

  1.     查詢的結果有關的資訊: 這包括由任何SELECT,UPDATE或DELETE語句產生數量的記錄。
  2.     表和資料庫有關的資訊: 這包括表和資料庫的結構有關的資訊。
  3.     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,資料庫伺服器擷取各種重要訊息。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.