当我最近想在 php 中列出数据库中的所有表时,我做了一个简单的 MySQL 查询:
$tables_query = mysql_query('show tables');
while ($test = mysql_fetch_array($tables_query)) {
echo "Table: {$test[0]}<br />";
}
第一个结果是
TABLES 105
address_book
我没有名为“TABLES 105”的表,但 mysql_num_rows 也显示有 105 个结果,即使我的数据库只包含 104 个表
如果我尝试直接在 MySql 服务器上请求“显示表”,它工作正常,结果我得到 104 行。它之前也有效,但我似乎找不到任何相关信息,所以我希望有人能在这里帮助我。
它也会影响我直接调用 mysql 服务器。我在同一台服务器上通过其他用户登录访问了其他数据库,这里完全没有问题。
最佳答案
105
最初是如何到达那里的,这很可能是由 mysql_num_rows
函数引起的,您提到的 fetch_array
实际上获取行,但这是 MySQLi 上的一个,不再使用 MySQL:
$db = 'test'; // database name
$con = mysqli_connect('localhost', 'username', 'password', $db);
$tables_query = mysqli_query($con, "SHOW TABLES FROM {$db}");
while($table = mysqli_fetch_assoc($tables_query)) {
echo $table["Tables_in_{$db}"], '<br/>';
}
当然,另一种方法是深入研究information_schema
:
$db = 'test'; // database name
$con = mysqli_connect('localhost', 'root', '', $db);
$tables_query = mysqli_query($con, "SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{$db}'");
while($table = mysqli_fetch_array($tables_query)) {
echo $table['TABLE_NAME'], '<br/>';
}
关于php - mysql Show tables 以 TABLES "number of rows"作为第一个值返回所有 +1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29794618/