PHP PDO显示表功能仅显示最新表并非全部

时间:2022-08-15 15:20:17

The following code is supposed to count the number of tables in the database. It only counts the latest table. As does my attempt to list the tables by name, only listing the latest. All the tables are just for testing and do not have any different attributes.

以下代码应该计算数据库中的表数。它只计算最新的表格。我试图按名称列出表格,只列出最新的表格。所有表仅用于测试,没有任何不同的属性。

function checkdbempty(){
    global $fsdbh;
    $results = $fsdbh->query("show tables");
    foreach($results as $result); { $int += 1; }
    return $int;
}

That will return 1 result.

这将返回1个结果。

function checkdbempty(){
    global $fsdbh;
    $check = $fsdbh->query('show tables')->fetch(PDO::FETCH_ASSOC);
    foreach($check as $ch){ echo $ch; }
}

This will tell me the name of the first table:

这将告诉我第一个表的名称:

function checkdbempty(){
    global $fsdbh;
    $check = $fsdbh->query('show tables')->fetch(PDO::FETCH_ASSOC); $result = '';
    foreach($check as $ch){ $result.= $ch; }
    return $result;
}

And it will count only one with ->fetch(PDO::FETCH_NUM);

并且它只计算一个 - > fetch(PDO :: FETCH_NUM);

What is the problem

问题是什么

3 个解决方案

#1


0  

Try this:

$sth = $fsdbh->prepare('show tables');
$sth->execute();
return $sth->rowCount();

#2


1  

foreach($results as $result); { $int += 1; }

foreach is terminated by semicolon, the code after it is executed only once.

foreach以分号结束,代码只执行一次。

 $check = $fsdbh->query('show tables')->fetch(PDO::FETCH_ASSOC);

$check is single row fetched from statement, if you want to traverse every row use fetchAll method

$ check是从语句中获取的单行,如果要遍历每一行使用fetchAll方法

#3


1  

fetch() will fetch only one row, and is not used in method chaining so much. You want to fetchAll() instead. When you use PDO::FETCH_NUM, you are fetching the columns with numeric indices rather than column names in an associative array -- you are not retrieving the number of rows returned by the query

fetch()只能获取一行,并且不会在方法链接中使用太多。你想改为fetchAll()。当您使用PDO :: FETCH_NUM时,您将获取具有数字索引而不是关联数组中的列名称的列 - 您不检索查询返回的行数

$check = $fsdbh->query('show tables')->fetchAll(PDO::FETCH_ASSOC);
var_dump($check);

It is unwise to chain methods in this manner, even though show tables is known to be syntactically correct and should not fail in the RDBMS. Instead, check for a FALSE return then fetch:

以这种方式链接方法是不明智的,即使已知show table在语法上是正确的并且不应该在RDBMS中失败。相反,检查FALSE返回然后获取:

$result = $fsdbh->query('show tables');
if ($result) {
  $check = $result->fetchAll(PDO::FETCH_ASSOC);
}

Using fetch() for single rows assumes you will fetch inside a while loop:

对单行使用fetch()假设您将在while循环中获取:

$rows = array();
$results = $fsdbj->query('show tables');
if ($results){
  while ($row = $results->fetch(PDO::FETCH_ASSOC)) {
    $rows[] = $row;
  }
}

#1


0  

Try this:

$sth = $fsdbh->prepare('show tables');
$sth->execute();
return $sth->rowCount();

#2


1  

foreach($results as $result); { $int += 1; }

foreach is terminated by semicolon, the code after it is executed only once.

foreach以分号结束,代码只执行一次。

 $check = $fsdbh->query('show tables')->fetch(PDO::FETCH_ASSOC);

$check is single row fetched from statement, if you want to traverse every row use fetchAll method

$ check是从语句中获取的单行,如果要遍历每一行使用fetchAll方法

#3


1  

fetch() will fetch only one row, and is not used in method chaining so much. You want to fetchAll() instead. When you use PDO::FETCH_NUM, you are fetching the columns with numeric indices rather than column names in an associative array -- you are not retrieving the number of rows returned by the query

fetch()只能获取一行,并且不会在方法链接中使用太多。你想改为fetchAll()。当您使用PDO :: FETCH_NUM时,您将获取具有数字索引而不是关联数组中的列名称的列 - 您不检索查询返回的行数

$check = $fsdbh->query('show tables')->fetchAll(PDO::FETCH_ASSOC);
var_dump($check);

It is unwise to chain methods in this manner, even though show tables is known to be syntactically correct and should not fail in the RDBMS. Instead, check for a FALSE return then fetch:

以这种方式链接方法是不明智的,即使已知show table在语法上是正确的并且不应该在RDBMS中失败。相反,检查FALSE返回然后获取:

$result = $fsdbh->query('show tables');
if ($result) {
  $check = $result->fetchAll(PDO::FETCH_ASSOC);
}

Using fetch() for single rows assumes you will fetch inside a while loop:

对单行使用fetch()假设您将在while循环中获取:

$rows = array();
$results = $fsdbj->query('show tables');
if ($results){
  while ($row = $results->fetch(PDO::FETCH_ASSOC)) {
    $rows[] = $row;
  }
}