Software, your way.
burger menu icon
WillMaster

WillMaster > LibraryWebsite Development and Maintenance

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Database Table Dump

When it was needed for a project, I made something to retrieve the content of a MySQL table and put it on a web page. It was where I had FTP access but no domain control panel access.

I called the software Database Table Dump. For visual inspection, it created an HTML table with the column names along the top. The table rows contained the database rows of data. For importing into my spreadsheet, it downloaded the table column names and data as a CSV file.

I thought perhaps some Possibilities subscribers could use it, too.

The PHP script is over 200 lines long. It does not require customization; in other words, just upload and use.

Here is the control panel you'll see when you load Database Table Dump into your browser.

screenshot of Database Table Dump.

Provide the MySQL credentials, including the table name, and tap the Dump Database button. If you want to download a CSV file, tap its radio button before tapping the Dump Database button.

Here is a screenshot of a dump from a table on my development computer.

screenshot of Database Table Dump.

For convenience when you have large tables, the gray-background table head remains at the top of the window as the table is scrolled upward past that point.

Here is the source code of the Database Table Dump software.

<?php
/*
Database Table Dump
Dashboard
Version 1.0
July 18, 2025
Will Bontrager Software LLC
https://www.willmaster.com/
*/
$MySQL = false;
$Structure = array();
$Tablename = '';
class MySQLclass
{
   private $MySQLi;
   private $result;
   public $numRowsInResult;
   public $lastID;
   public $error;
   function __construct()
   {
      $this->numRowsInResult = 0;
      $this->lastID = 0;
      $this->error = array();
      $th = func_get_args();
      $this->MySQLi = new mysqli( $th[0]['hostname'], $th[0]['username'], $th[0]['password'], $th[0]['database'] );
      if( $this->MySQLi->connect_errno ) { $this->error[] = 'Unable to connect to the database server (error number '.$this->MySQLi->connect_errno.'):<br>'.$this->MySQLi->connect_error; }
      if( ! $this->MySQLi->set_charset('utf8') ) { $this->error[] = $this->AppendErrorNotationToErrorMessage('Error loading character set utf8'); }
      return $this->error;
   }
   function __destruct() { $this->MySQLi->close(); }
   public function ErrorMessage() { return $this->error; }
   public function GetTableStructure($table)
   {
      $struct = array();
      $sql = "SHOW COLUMNS FROM $table";
      if( ! $result = @mysqli_query($this->MySQLi,$sql) )
      {
         $this->error[] = "Unable to show columns for table $table";
         return false;
      }
      while ($row = mysqli_fetch_assoc($result))
      {
         $struct[] = $row;
         #$struct[] = $row['Field']."\t".$row['Type']."\t".$row['Null']."\t".$row['Key']."\t".$row['Default']."\t".$row['Extra'];
      }
      return $struct;
   }
   public function GetRow()
   {
      if( ($row = mysqli_fetch_assoc($this->result)) ) { return $row; }
      return array();
   }
   public function SelectRecordData($arr)
   {
      $this->error = array();
      if( empty($arr['tablename']) )
      {
         $this->AppendErrorNotationToErrorMessage('Table name is required.');
         return false;
      }
      $sql = 'SELECT ';
      if( isset($arr['cols']) )
      {
         if( is_array($arr['cols']) ) { $sql .= implode(',',$arr['cols']) . ' '; }
         else { $sql .= implode(',',preg_split('/[, ]+/',$arr['cols'])) . ' '; }
      }
      else { $sql .= '* '; }
      $sql .= "FROM {$arr['tablename']} ";
      if( isset($arr['where']) ) { $sql .= "WHERE {$arr['where']} "; }
      if( isset($arr['order']) ) { $sql .= "ORDER BY {$arr['order']} "; }
      $limit = isset($arr['limit']) ? preg_replace('/[^\d\,]+/','',$arr['limit']) : 0;
      $offset = isset($arr['offset']) ? intval($arr['offset']) : 0;
      if( $offset and (! $limit ) ) { $limit = 9999999; }
      if( $limit ) { $sql .= "LIMIT $limit "; }
      if( $offset ) { $sql .= "OFFSET $offset "; }
      if( ($this->result = @mysqli_query($this->MySQLi,$sql)) )
      {
         $this->numRowsInResult = mysqli_num_rows($this->result);
         return true;
      }
      $this->AppendErrorNotationToErrorMessage('Selection failed.');
      return false;
   }
   public function FreeResult() { $this->result->close(); }
} # class MySQLclass
if(isset($_POST['dumpit']))
{
   $hostname=preg_match('/\w/',$_POST['hostname'])?$_POST['hostname']:'localhost';
   $username=preg_match('/\w/',$_POST['username'])?$_POST['username']:'t';
   $password=preg_match('/\w/',$_POST['password'])?$_POST['password']:'t';
   $database=preg_match('/\w/',$_POST['database'])?$_POST['database']:'t';
   $table=preg_match('/\w/',$_POST['table'])?$_POST['table']:'al1_structures';
   if( empty($_POST['destination']) ) { $_POST['destination'] = 'html'; }
}
if(isset($_POST['dumpit']) and isset($_POST['dumpit']) and isset($_POST['hostname']) and isset($_POST['username']) and isset($_POST['password']) and isset($_POST['database']) and isset($_POST['table']) )
{
   $Tablename = $table;
   $MySQL = new MySQLclass( array( 'hostname'=>$hostname, 'username'=>$username, 'password'=>$password, 'database'=>$database ) );
   $th = $MySQL->GetTableStructure($table);
   foreach( $th as $arr ) { $Structure[] = $arr['Field']; }
   if(count($Structure) and $_POST['destination']=='csv')
   {
      header('Content-Type: text/csv; charset=utf-8');
      header("Content-Disposition: attachment; filename=$Tablename-" . preg_replace('/__+/','_',(preg_replace('/\W+/','_',date('r')))) . '.csv');
      $successArray = array(
         'cols' => array('*'), 
         'tablename' => $Tablename, 
      );
      $success = $MySQL->SelectRecordData( $successArray );
      if( $success )
      {
         echo(MakeCSVline($Structure)."\r\n");
         while( ( $row = $MySQL->GetRow() ) ) { echo(MakeCSVline(array_values($row))."\r\n"); }
      }
      else { echo('Unable to read MySQL file'); }
      exit;
      $MySQL->FreeResult();
   }
}
function MakeCSVvalue($s) { return ('"' . str_replace('"','""',$s) . '"'); }
function MakeCSVline($arr)
{
   $count = count($arr);
   for( $i=0; $i<$count; $i++ ) { $arr[$i] = MakeCSVvalue($arr[$i]); }
   return implode(',',$arr);
}
if( empty($_POST['destination']) ) { $_POST['destination'] = 'html'; }
?><!DOCTYPE html>
<html lang="en">
<head> 
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Database Table Dump</title>
<style type="text/css">
@charset "utf-8";
* { box-sizing:border-box; }
html { font-family:sans-serif; font-size:100%; }
.nowrap { white-space:nowrap; }
input[type="radio"] { margin:0; vertical-align:.2em; }
input[type="text"], input[type="submit"] { width:100%; }
input[type="text"], textarea { border:1px solid #ccc; border-radius:.25em; padding:.25em .5em; }
tr { vertical-align:bottom; font-size:85%; }
td { vertical-align:top; }
</style>
</head>
<body>
<form id="my-form" enctype="multipart/form-data" action="<?php echo($_SERVER['PHP_SELF']) ?>" method="post" accept-charset="utf-8">
<div style="width:fit-content; margin:.5in auto 1.5rem; auto; max-width:5.5in;">
<h1 style="margin-top:0;">Database Table Dump</h1>
<p>
Host Name:<br>
<input type="text" name="hostname" value="<?php echo(isset($_POST['hostname'])?$_POST['hostname']:'') ?>">
</p>
<p>
Database:<br>
<input type="text" name="database" value="<?php echo(isset($_POST['database'])?$_POST['database']:'') ?>">
</p>
<p>
Table Name:<br>
<input type="text" name="table" value="<?php echo(isset($_POST['table'])?$_POST['table']:'') ?>">
</p>
<p>
Username:<br>
<input type="text" name="username" value="<?php echo(isset($_POST['username'])?$_POST['username']:'') ?>">
</p>
<p>
Password:<br>
<input type="text" name="password" value="<?php echo(isset($_POST['password'])?$_POST['password']:'') ?>">
</p>
<p style="line-height:150%; text-indent:-1em; margin-left:1em;">
Output:<br>
<label class="nowrap"><input type="radio" name="destination" value="html"<?php if($_POST['destination']=='html'){echo(' checked="checked"');} ?>>&thinsp;HTML</label> &nbsp; 
<label class="nowrap"><input type="radio" name="destination" value="csv"<?php if($_POST['destination']=='csv'){echo(' checked="checked"');} ?>>&thinsp;CSV</label>
</p>
<p style="margin-top:1.5em;">
<input type="submit" name="dumpit" value="Dump Database">
</p>
</div>
</form>
<?php if(count($Structure) and $_POST['destination']=='html'): ?>
<table border="1" cellpadding="5" cellspacing="0" style="border-collapse:collapse; empty-cells:show; margin-bottom:2em;">
<?php $headcount = count($Structure); ?>
<tr style="background-color:transparent;">
<th colspan="<?php echo($headcount) ?>" style="text-align:left;">Table: <?php echo($Tablename) ?></th>
</tr><tr style="position:sticky; top:0px;">
<th style="padding:0;"><div style="height:2em; background-color:#efefef; padding:.5em 8px 0  8px;"><?php echo( implode('</th><th style="padding:0;"><div style="height:2em; background-color:#efefef; padding:.5em 8px 0  8px;">',$Structure) ) ?></th>
</tr>
<?php
$successArray = array(
   'cols' => array('*'), 
   'tablename' => $Tablename, 
);
$success = $MySQL->SelectRecordData( $successArray );
if( $success )
{
   while( ( $row = $MySQL->GetRow() ) ) { echo( '<tr><td>' . implode('</td><td>',$row) . '</td></tr>' ); }
}
$MySQL->FreeResult();
?>
</table>
<?php endif; ?>
<p style="text-align:center;">
Created by <a href="https://www.willmaster.com/">Will Bontrager Software LLC
</p>
</body>
</html>

In most cases, perhaps all, Database Table Dump will need to be installed on the same domain as where the MySQL database reside. It may be uploaded as-is; no customization is required.

Database Table Dump can be a handy script when you need to inspect database table information, yet don't have access to the domain control panel — or just don't want to take the time to use it. Provide the MySQL credentials. Database Table Dump will show you its column names and content.

(This content first appeared in Possibilities newsletter.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2025 Will Bontrager Software LLC