简体   繁体   English

打印出25个最近添加的表

[英]Printing out 25 most recently added tables

In the code below, I allow users to add tables to a MySQL database. 在下面的代码中,我允许用户将表添加到MySQL数据库。 Is there a way to print out the most recent 25 tables added? 有没有办法打印出最近添加的25个表?

Thanks in advance, 提前致谢,

John 约翰

$table = mysql_real_escape_string($_POST['name']);

$query = "CREATE TABLE `$table` (id INT(11) NOT NULL auto_increment, site VARCHAR(350) NOT NULL, cat1 BIGINT(9) NOT NULL, cat2 BIGINT(9) NOT NULL, PRIMARY KEY(id), UNIQUE (site))";
$result = mysql_query($query) or die(mysql_error());
select TABLE_NAME
  from INFORMATION_SCHEMA.TABLES
 where TABLE_SCHEMA='your_db_name`
 order by CREATE_TIME desc
 limit 25

This is for MySQL version 5 and above. 这适用于MySQL 5及更高版本。 It will list tables created by your users and you, though. 不过,它将列出您的用户您创建的表。

You can but I'd advise creating a history or audit table of all the table creations (and other relevant activity). 您可以,但我建议您为所有表创建(和其他相关活动)创建历史记录或审计表。 For example: 例如:

$now = time();
$user = $_SESSION['user'];
$sql = <<<END
INSERT INTO table_history
(table, user, action, action_time)
VALUES
('$table', '$user', 'CREATE', $now)
END;
mysql_query($sql) or die($sql . ': ' . mysql_error());

Also, I'd probably suggest not creating a separate table for each instance you need to use it unless they're going to be really large. 另外,我可能建议不要为需要使用它的每个实例创建单独的表,除非它们真的很大。 Creating another column to identify the user, organization or whatever is often (but not always) a better approach. 创建另一列来标识用户,组织或其他通常(但并非总是)更好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM