从MySQL数据库显示PHP中的所有表名 [英] Displaying all table names in php from MySQL database

查看:297
本文介绍了从MySQL数据库显示PHP中的所有表名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我对PHP和SQL/MySQL还是很陌生,因此可以提供任何帮助.

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

我觉得我采取了正确的方法.我在php.net上搜索了"MySQL显示所有表名",它返回了不推荐使用的方法,并建议在SHOW TABLES [FROM db_name] [LIKE 'pattern']上使用MySQL查询.我不确定模式"是什么意思,但是,我搜索了"SQL通配符"并得到了%"符号.根据我发现的所有内容,这应该可以工作并在末尾输出表名称,但事实并非如此.有什么建议?预先感谢.

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
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.

推荐答案

您的代码中的方括号在mysql文档中用于指示可选参数组.它们不应在实际查询中.

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.

您真正需要的唯一命令是:

The only command you actually need is:

show tables;

如果您要从特定数据库中获取表,假设数据库为"books",那么它将是

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

show tables from books;

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

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

show tables from books like '%book%';

将显示名称中带有"book"的表的名称.

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

此外,仅运行显示表"查询将不会产生任何可见的输出. SQL回答查询,然后将其传递给PHP,但是您需要告诉PHP将其回显到页面.

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.

由于听起来您是SQL的新手,所以建议您从命令行运行mysql客户端(或者如果系统中已安装phpmyadmin,则使用phpmyadmin).这样,您就可以查看各种查询的结果,而不必通过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.

如果必须使用PHP,这是一个非常简单的演示.连接到数据库后,请尝试以下代码:

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

$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.
}

这篇关于从MySQL数据库显示PHP中的所有表名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆