Wu Yuxiong - natural born MySQL study notes: MySQL metadata

You may want to know the following three MySQL information:
The search result: SELECT, UPDATE, or the number of records affected by the DELETE statement.
Information database and data table: Database contains configuration information and data tables.
MySQL server information: contains the current status, and version number of the database server.
In the MySQL command prompt, you can easily get more server information. But if you use scripting languages ​​such as Perl or PHP, you need to call a specific interface function to obtain.
Gets the number of records affected by the query
In DBI script, the statement returns the number of records affected by the function do () or execute ():
# 1 
# using do () Query execution $ 
My $ COUNT = $ dbh-> do ($ Query);
 # If an error occurs the output 0 
the printf " % D pieces of data affected \ n- " , (defined ($ COUNT) ? $ count: 0);

# Method 2 
# use prepare () and execute () Query execution $ 
My $ STH = $ dbh-> PREPARE ($ Query);
my $count = $sth->execute ( );
printf " % d of data affected \ the n- " , (defined ($ COUNT) $ COUNT: 0?);
PHP example
In PHP, you can use mysqli_affected_rows () function to get the number of records affected by the query.
result_id $ = mysqli_query ($ conn_id, $ Query);
 # If the search fails to return 
$ COUNT = ($ result_id mysqli_affected_rows ($ conn_id):? 0);
 Print ( " $ COUNT pieces of data are affected \ the n- " );
Databases and data tables list
You can easily get a list of database tables and data in the MySQL server. If you do not have sufficient permissions, the results will return null.
You can also use SHOW TABLES or SHOW DATABASES statement to retrieve a list of databases and data tables.
PERL examples
# Get the current database of all available tables. 
= $ dbh- @tables My> Tables ();
foreach $table (@tables ){
   print "表名 $table\n";
}
PHP example
The following examples all databases on the MySQL server output:
<?php
dbHost $ = ' localhost: 3306 ' ; // MySQL server host address
dbuser $ = ' root ' ; // MySQL user name
dbpass $ = ' 123456 ' ; // MySQL user name and password
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
    Die ( ' connection failed: ' mysqli_error ($ Conn).);
}
// set the encoding to prevent Chinese garbled
$db_list = mysqli_query($conn, 'SHOW DATABASES');
while ($db = mysqli_fetch_object($db_list))
{
  echo $db->Database . "<br />";
}
mysqli_close($conn);
?>
Get the server metadata
The following command syntax can be used in the MySQL command prompt can also be used in the script, such as PHP scripts.

 

Guess you like

Origin www.cnblogs.com/tszr/p/12114559.html