You may want to know the following three messages about MySQL:
查询结果信息: The number of records affected by the SELECT, UPDATE, or DELETE statement.
数据库和数据表的信息: It contains the structure information of database and data table.
MySQL服务器信息: Contains the current status of the database server, version number, and so on.
From the MySQL command prompt, we can easily get the above server information. But if you use a scripting language such as Perl or PHP, you need to call a specific interface function to get it. We will introduce it in more detail next. In the DBI script, the number of records affected by the statement is returned through the function do () or execute (): In PHP, you can use the You can easily get lists of databases and tables in the MySQL server. If you do not have sufficient permissions, the result will return null. You can also use SHOW TABLES or SHOW DATABASES statements to get lists of databases and tables. The following example outputs all databases on the MySQL server: View all databases The following command statements can be used at the command prompt in MySQL or in scripts, such as PHP scripts. Command Description SELECT VERSION () Server version information SELECT DATABASE () Current database name (or return empty) SELECT USER () Current user name SHOW STATUS Server statu SHOW VARIABLES Server configuration variableGet the number of records affected by the query statement ¶
PERL instance ¶
# 方法 1
# 使用do( ) 执行 $query
my $count = $dbh->do ($query);
# 如果发生错误会输出 0
printf "%d 条数据被影响\n", (defined ($count) ? $count : 0);
# 方法 2
# 使用prepare( ) 及 execute( ) 执行 $query
my $sth = $dbh->prepare ($query);
my $count = $sth->execute ( );
printf "%d 条数据被影响\n", (defined ($count) ? $count : 0);
PHP instance ¶
mysqli_affected_rows(
)
Function to get the number of records affected by the query statement.$result_id = mysqli_query ($conn_id, $query);
# 如果查询失败返回
$count = ($result_id ? mysqli_affected_rows ($conn_id) : 0);
print ("$count 条数据被影响\n");
List of databases and datasheets ¶
PERL instance ¶
# 获取当前数据库中所有可用的表。
my @tables = $dbh->tables ( );
foreach $table (@tables ){
print "表名 $table\n";
}
PHP instance ¶
<?php$dbhost='localhost';//mysql服务器主机地址$dbuser='root';//mysql用户名$dbpass='123456';//mysql用户名密码$conn=mysqli_connect($dbhost,$dbuser,$dbpass);if(!$conn){die('连接失败:'.mysqli_error($conn));}//设置编码,防止中文乱码$db_list=mysqli_query($conn,'SHOW
DATABASES');while($db=mysqli_fetch_object($db_list)){echo$db->Database."<br
/>";}mysqli_close($conn);?>
Get server metadata ¶