Home > Database > Mysql Tutorial > What is MySQL metadata? Introduction to metadata and example code

What is MySQL metadata? Introduction to metadata and example code

零下一度
Release: 2017-05-16 10:53:43
Original
2742 people have browsed it

MySQL Metadata

You may want to know the following three types of information about MySQL:

Query result information: The number of records affected by the SELECT, UPDATE or DELETE statement.

Database and data table information: Contains structural information of the database and data table.

MySQL server information: Contains the current status, version number, etc. of the database server.

In the MySQL command prompt, we can easily obtain the above server information. But if you use a scripting language such as Perl or PHP, you need to call a specific interfacefunction to obtain it. We will introduce it in detail next.

Get the number of records affected by the query statement

PERL example

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

# 方法 1
# 使用do( ) 执行  $query 
my $count = $dbh->do ($query);
# 如果发生错误会输出 0
printf "%d rows were affected\n", (defined ($count) ? $count : 0);
# 方法 2
# 使用prepare( ) 及 execute( ) 执行  $query 
my $sth = $dbh->prepare ($query);
my $count = $sth->execute ( );
printf "%d rows were affected\n", (defined ($count) ? $count : 0);
Copy after login

PHP Example

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

$result_id = mysql_query ($query, $conn_id);
# 如果查询失败返回 $count = ($result_id ? mysql_affected_rows ($conn_id) : 0);
print ("$count rows were affected\n");
Copy after login

Database and data table list

You can easily get the database and data table list in the MySQL server. If you do not have sufficient permissions, the result will be null.

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

PERL Example

# Get all available tables in the current database.

my @tables = $dbh->tables ( );
foreach $table (@tables ){
   print "Table Name $table\n";
}
Copy after login

PHP Example

<?php
$con = mysql_connect("localhost", "userid", "password");
if (!$con)
{
  die(&#39;Could not connect: &#39; . mysql_error());
}
$db_list = mysql_list_dbs($con);
while ($db = mysql_fetch_object($db_list))
{
  echo $db->Database . "<br />";
}
mysql_close($con);
?>
Copy after login

【Related Recommendations】

1. Special Recommendations:"php Programmer Toolbox" V0.1 version download

2. Free mysql online video tutorial

3. Those things about database design

The above is the detailed content of What is MySQL metadata? Introduction to metadata and example code. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template