简体   繁体   English

如何从PHP转储整个MySQL内容?

[英]How to dump whole MySQL contents from PHP?

Is there a clean way (no hardcoding) in which I can dump all the contents of a database directly to HTML using PHP? 有没有一种干净的方法(无需硬编码),可以使用PHP将数据库的所有内容直接转储为HTML?

I don't want to run queries for every table and step through the results, outputting them. 我不想为每个表运行查询并单步执行结果,将其输出。 I need this for testing purposes, so the tables aren't that big. 我出于测试目的需要此表,因此表并不大。

Any hints? 有什么提示吗?

I want this done directly in my php file, where the rest of the test takes place, so that I may compare with the sample. 我希望直接在我的php文件中完成此操作,其余的测试将在该文件中进行,以便与示例进行比较。 I need to do this automatically, so can't really use tools like PHPMyAdmin. 我需要自动执行此操作,因此不能真正使用PHPMyAdmin之类的工具。

Something like this ought to work: 这样的事情应该起作用:

<?php

function dump_mysql_results($mysql_table){
    $query = mysql_query("SELECT * FROM `$table` WHERE 1",[your connection]) or die(mysql_error());
    if (!mysql_num_rows($query)){die("No rows in $table");}
    while($r=mysql_fetch_array($query)){
        if (!isset($html)){
            $keys = array_keys($r); 
            $html = "<tr>";
            foreach($keys as $key){
                $html .= "<th>$key</th>";
            }
            $html .= "</tr>";
        }
        $html .= "<tr>";
        foreach($r as $value){
            $html .= "<td>$value</td>";
        }
        $html .= "</tr>";   
    }
    return "<table>".$html."</table>";
}

//ADDING a loop to dump the whole db: //添加循环以转储整个数据库:

$tables = mysql_list_tables ( 'database name',$link_identifier) or die(mysql_error());
while($r=mysql_fetch_array($tables)){
    echo dump_mysql_results($r[0]);
}

?>

Use SHOW TABLES to get the list of tables, then iterate through them normally to select all the rows and display in HTML. 使用SHOW TABLES获取表列表,然后正常地遍历它们以选择所有行并以HTML显示。

See http://dev.mysql.com/doc/refman/5.5/en/show-tables.html http://dev.mysql.com/doc/refman/5.5/en/show-tables.html

What about mysqldump ? mysqldump怎么样?

<?php
 exec('mysqldump --user=DBuser --password=DBpass --host=localhost --compact --xml DBname > file.xml');
?>

Then use simpleXML to convert xml to HTML 然后使用simpleXML将xml转换为HTML

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

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