php 从MySQL数据库显示php中的所有表名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9898610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:52:42  来源:igfitidea点击:

Displaying all table names in php from MySQL database

phpmysqlsql

提问by EGHDK

Alright, so I'm fairly new to PHP and SQL/MySQL so any help is appreciated.

好的,所以我对 PHP 和 SQL/MySQL 还很陌生,因此感谢您提供任何帮助。

I feel like I took the right approach. I searched php.net for "MySQL show all table names", it returned a deprecated method and suggested using a MySQL query on SHOW TABLES [FROM db_name] [LIKE 'pattern']I'm not sure what "pattern" means but, I searched for "SQL Wildcard" and got the "%" symbol. According to everything I found, this should work and output the table names at the end, but it does not. Any suggestions? Thanks in advance.

我觉得我采取了正确的方法。我在 php.net 中搜索了“MySQL 显示所有表名”,它返回了一个不推荐使用的方法,并建议在SHOW TABLES [FROM db_name] [LIKE 'pattern']我不确定“模式”是什么意思上使用 MySQL 查询,但是,我搜索了“SQL 通配符”并得到了“% “ 象征。根据我发现的一切,这应该可以工作并在最后输出表名,但事实并非如此。有什么建议?提前致谢。

<?php
if ($_REQUEST["username"]=="coke"&&$_REQUEST["password"]=="pepsi"){
echo 'You have successfully logged in.';
echo '<br />';
echo 'These are your tables:';
echo '<br />';

   $link = mysql_connect("sql2.njit.edu", "username", "password");

   mysql_select_db("db_name") or die(mysql_error());

   $result = mysql_query('SHOW TABLES [FROM db_name] [LIKE '%']');
   echo $result;
}
else
echo 'You did not provide the proper authentication';
?>

I get no errors. The output is exactly what's echoed, but no table names.

我没有错误。输出正是回显的内容,但没有表名。

回答by octern

The square brackets in your code are used in the mysql documentation to indicate groups of optional parameters. They should not be in the actual query.

mysql 文档中使用代码中的方括号来指示可选参数组。它们不应该出现在实际查询中。

The only command you actually need is:

您实际需要的唯一命令是:

show tables;

If you want tables from a specific database, let's say the database "books", then it would be

如果您想要来自特定数据库的表,假设数据库“书籍”,那么它将是

show tables from books;

You only need the LIKE part if you want to find tables whose names match a certain pattern. e.g.,

如果要查找名称与特定模式匹配的表,则只需要 LIKE 部分。例如,

show tables from books like '%book%';

would show you the names of tables that have "book" somewhere in the name.

将向您显示名称中某处带有“book”的表的名称。

Furthermore, just running the "show tables" query will not produce any output that you can see. SQL answers the query and then passes it to PHP, but you need to tell PHP to echo it to the page.

此外,仅运行“show tables”查询不会产生您可以看到的任何输出。SQL 回答查询,然后将其传递给 PHP,但您需要告诉 PHP 将其回显到页面。

Since it sounds like you're very new to SQL, I'd recommend running the mysql client from the command line (or using phpmyadmin, if it's installed on your system). That way you can see the results of various queries without having to go through PHP's functions for sending queries and receiving results.

由于听起来您对 SQL 很陌生,我建议您从命令行运行 mysql 客户端(或使用 phpmyadmin,如果您的系统上安装了它)。这样您就可以看到各种查询的结果,而无需通过 PHP 的发送查询和接收结果的功能。

If you have to use PHP, here's a very simple demonstration. Try this code after connecting to your database:

如果你必须使用PHP,这里有一个非常简单的演示。连接到数据库后试试这个代码:

$result = mysql_query("show tables"); // run the query and assign the result to $result
while($table = mysql_fetch_array($result)) { // go through each row that was returned in $result
    echo($table[0] . "<BR>");    // print the table that was returned on that row.
}

回答by Sharpless512

For people that are using PDO statements

对于使用PDO 语句的人

$query = $db->prepare('show tables');
$query->execute();

while($rows = $query->fetch(PDO::FETCH_ASSOC)){
     var_dump($rows);
}

回答by Joshua Martell

Queries should look like :

查询应如下所示:

SHOW TABLES

SHOW TABLES FROM mydatabase

SHOW TABLES FROM mydatabase LIKE "tab%"

Things from the MySQL documentation in square brackets [] are optional.

方括号 [] 中 MySQL 文档中的内容是可选的。

回答by marvin

SHOW TABLES

will show all the tables in your db. If you want to filter the names you use LIKEand wildcard %

将显示您数据库中的所有表。如果要过滤使用的名称LIKE和通配符%

SHOW TABLES FROM my_database LIKE '%user%'

will give you all tables that's name include 'user', for example

将为您提供名称包括“用户”的所有表,例如

users
user_pictures
mac_users

etc.

等等。

回答by marvin

The brackets that are commonly used in the mysql documentation for examples should be ommitted in a 'real' query.

mysql 文档中常用的括号示例应该在“真实”查询中省略。

It also doesn't appear that you're echoing the result of the mysql query anywhere. mysql_queryreturns a mysql resource on success. The php manual page also includes instructions on how to load the mysql result resource into an array for echoing and other manipulation.

您似乎也没有在任何地方回显 mysql 查询的结果。 mysql_query在成功时返回一个 mysql 资源。php 手册页还包括有关如何将 mysql 结果资源加载到数组中以进行回显和其他操作的说明。

回答by Al_

you need to assign the mysql_query to a variable (eg $result), then display this variable as you would a normal result from the database.

您需要将 mysql_query 分配给一个变量(例如 $result),然后像显示数据库中的正常结果一样显示该变量。

回答by braumer

Sure you can query your Database with SHOW TABLESand then loop through all the records but that is extra code lines and work.

当然你可以查询你的数据库,SHOW TABLES然后遍历所有记录,但这是额外的代码行和工作。

PHP has a built in function to list all tables into an array for you :

PHP 有一个内置函数,可以为您将所有表列出到一个数组中:

mysql_list_tables - you can find more information about it at The PHP API page

mysql_list_tables - 您可以在PHP API 页面上找到有关它的更多信息

回答by Mizanur Rahaman

//list_tables means database all table

//list_tables 表示数据库所有表

$tables = $this->db->list_tables();
foreach ($tables as $table)
{
  echo $table;
}