MySQL metadata viewing and instance code, mysql metadata

Source: Internet
Author: User

MySQL metadata viewing and instance code, mysql metadata

You may want to know the following information about MySQL:

  • Query Result information: number of records affected by SELECT, UPDATE, or DELETE statements.
  • Database and data table information: contains the structure information of the database and data table.
  • MySQL Server Information: contains the current status and version number of the database server.

In the MySQL command prompt, we can easily obtain the above server information. However, if you use Perl, PHP, and other scripting languages, you need to call specific interface functions to obtain them. Next we will introduce in detail.

 Obtain the number of records affected by the query statement.

PERL instance

In the DBI script, the number of records affected by the statement is returned through the do () or execute () function:

# Method 1 # Run $ query my $ count = $ dbh-> do ($ query) using do ); # If an error occurs, 0 printf "% d rows were affected \ n", (defined ($ count )? $ Count: 0); # method 2 # Use prepare () and execute () to execute $ query my $ th = $ dbh-> prepare ($ query ); my $ count = $ something-> execute (); printf "% d rows were affected \ n", (defined ($ count )? $ Count: 0 );

PHP instance

In PHP, you can use the mysql_affected_rows () function to obtain the number of records affected by the query statement.

$ Result_id = mysql_query ($ query, $ conn_id); # returns $ count = ($ result_id? Mysql_affected_rows ($ conn_id): 0); print ("$ count rows were affected \ n ");

Database and data table list

You can easily obtain the list of databases and data tables on the MySQL server. If you do not have sufficient permissions, null is returned.

You can also use the show tables or show databases statement to obtain the list of DATABASES and data TABLES.

PERL instance

# Obtain all available tables in the 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 . "<br />";}mysql_close($con);?> 

Get server metadata

The following command can be used at a MySQL Command Prompt or in a script, such as a PHP script.

Command Description
Select version () Server version information
Select database () Current Database Name (or return NULL)
Select user () Current User Name
SHOW STATUS Server Status
SHOW VARIABLES Server configuration variables

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

Related Article

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.