Reputation: 258558
Is there a clean way (no hardcoding) in which I can dump all the contents of a database directly to HTML using PHP?
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. I need to do this automatically, so can't really use tools like PHPMyAdmin.
Upvotes: 0
Views: 142
Reputation: 3608
What about mysqldump ?
<?php
exec('mysqldump --user=DBuser --password=DBpass --host=localhost --compact --xml DBname > file.xml');
?>
Then use simpleXML
to convert xml to HTML
Upvotes: 0
Reputation: 753
Use SHOW TABLES to get the list of tables, then iterate through them normally to select all the rows and display in HTML.
See http://dev.mysql.com/doc/refman/5.5/en/show-tables.html
Upvotes: 0
Reputation: 14479
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]);
}
?>
Upvotes: 1