Method for calling database metadata in MySQL _ MySQL

Source: Internet
Author: User
This article mainly introduces how to call the database metadata in MySQL. It provides the call examples in PHP and Perl scripts. For more information, see MySQL:

  1. Query result information: This includes the number of records generated by any SELECT, UPDATE, or DELETE statements.
  2. Table and database information: this includes table and database structure information.
  3. MySQL server information: This includes the current database server and version number.

It is easy to obtain all the information at the mysql prompt. However, when using Perl or PHP APIs, we need to explicitly call various APIs to obtain all this information. The following sections will show you how to obtain the information.
Obtain the number of rows affected by the query:
PERL instance:

In the DBI script, the number of affected rows is returned by the do () or execute () method, depending on how the query is executed:

# 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 instance:

In PHP, call the mysql_affected_rows () function to find out how many rows the query has changed:

$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");

Table and database list (list ):

It is easy to list all databases and tables associated with database servers. If you do not have sufficient permissions, the result may be blank.

In addition to the methods, I mentioned the following table or database list that can be queried using SHOW TABLES or SHOW DATABASES, whether in PHP or PERL.
PERL instance:

# Get all the tables available in current database.my @tables = $dbh->tables ( );foreach $table (@tables ){  print "Table Name $table\n";}

PHP instance:

<?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 . "
";}mysql_close($con);?>

Get the server metadata:

In MySQL, you can run the following command to obtain important information from a mysql prompt or any script, such as PHP or database server.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.