数据库中的PDO计数表 [英] PDO Count tables in database
问题描述
以下功能旨在获取数据库中的表.
The following function is designed to fetch the tables in the database.
$check = $fsdbh->query('show tables')->fetch();
无法计数,因为其中有一个额外的层,即数据库名称.
It cannot be counted because it has an extra layer in it, the database name.
print_r():
print_r():
Array ( [Tables_in_dbtest] => test [0] => test )
因此,我们需要更进一步,只计算数据库的数量.我们该怎么做.
So We need to get one step further into the array and just count the number of databases. How would we do that.
推荐答案
默认获取样式为PDO::FETCH_BOTH
,您需要的是PDO::FETCH_ASSOC
或PDO::FETCH_NUM
.
The default fetch style is PDO::FETCH_BOTH
, what you need is PDO::FETCH_ASSOC
or PDO::FETCH_NUM
.
有关更多信息,请参见 fetch
文档.
See the fetch
documentation for more information.
此代码应为您工作:
$check = $fsdbh->query('show tables')->fetch(PDO::FETCH_NUM);
您可以做的另一件事是使用 PDO:来更改默认的获取样式:连接到数据库后,将setAttribute 设置为PDO::FETCH_ASSOC
.
Another thing you could do is to change the default fetch style with PDO::setAttribute to PDO::FETCH_ASSOC
after you connect to your database.
$dbh = new PDO('...');
$dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
这篇关于数据库中的PDO计数表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!