Method of invoking database metadata in Mysql _mysql

Source: Internet
Author: User
Tags prepare

Three information on MySQL:

    1. Information about the results of the query: This includes the number of records produced by any select,update or DELETE statement.
    2. Table and Database information: This includes information about the structure of the table and the database.
    3. MySQL Server information: This includes the current state of the database server, version number, and so on.

All of this information is readily available at the MySQL prompt. However, when using Perl or PHP APIs, we need to explicitly invoke various APIs to get all this information. The following sections will show you how to obtain this information.
Get the number of rows affected by the query:
PERL Example:

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

# method 1
# Execute $query the Using do () my
$count = $dbh->do ($query);
# 0 Rows If an error occurred
printf "%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 of query changes:

$result _id = mysql_query ($query, $conn _id);
# 0 Rows If the query failed
$count = ($result _id mysql_affected_rows ($conn _id): 0);
Print ("$count rows were affected\n");

Tables and database listings (list):

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

In addition to the method, I mentioned below a list of tables or databases that can be queried using show tables or show databases, whether in PHP or Perl.
PERL Example:

# get all of 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. "<br/>";
}
Mysql_close ($con);
? >

Get metadata for server:

In MySQL you can execute the following commands that are used at the MySQL prompt or use any script such as PHP, database server to get all kinds of important information.

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.